Switch to ordered_json and enable ignore_comments parser option

This commit is contained in:
Jon Evans 2020-08-01 13:13:36 -04:00
parent 0d70cb70ea
commit aae40148dc
4 changed files with 19 additions and 58 deletions

View File

@ -194,7 +194,9 @@ bool JSON_SETTINGS::LoadFromFile( const std::string& aDirectory )
try try
{ {
FILE* fp = wxFopen( path.GetFullPath(), wxT( "rt" ) ); FILE* fp = wxFopen( path.GetFullPath(), wxT( "rt" ) );
*static_cast<nlohmann::json*>( this ) = nlohmann::json::parse( fp ); *static_cast<nlohmann::json*>( this ) = nlohmann::json::parse( fp, nullptr,
/* allow_exceptions = */ true,
/* ignore_comments = */ true );
// If parse succeeds, check if schema migration is required // If parse succeeds, check if schema migration is required
int filever = -1; int filever = -1;

View File

@ -1,41 +0,0 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef _KICAD_JSON_H
#define _KICAD_JSON_H
#include <nlohmann/json.hpp>
#include <nlohmann/fifo_map.hpp>
// This implements a version of nlohmann::basic_json that retains insertion ordering of objects
// See: https://github.com/nlohmann/json/issues/485#issuecomment-333652309
namespace kicad
{
template <class K, class V, class dummy_compare, class A>
using json_fifo_map = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
using json = nlohmann::basic_json<json_fifo_map>;
}
#endif

View File

@ -192,7 +192,7 @@ bool GERBER_JOBFILE_WRITER::WriteJSONJobFile( const wxString& aFullFilename )
LOCALE_IO dummy; LOCALE_IO dummy;
m_json = json( {} ); m_json = nlohmann::ordered_json( {} );
// output the job file header // output the job file header
addJSONHeader(); addJSONHeader();
@ -234,8 +234,8 @@ double GERBER_JOBFILE_WRITER::mapValue( double aUiValue )
void GERBER_JOBFILE_WRITER::addJSONGeneralSpecs() void GERBER_JOBFILE_WRITER::addJSONGeneralSpecs()
{ {
m_json["GeneralSpecs"] = json( {} ); m_json["GeneralSpecs"] = nlohmann::ordered_json( {} );
m_json["GeneralSpecs"]["ProjectId"] = json( {} ); m_json["GeneralSpecs"]["ProjectId"] = nlohmann::ordered_json( {} );
// Creates the ProjectId. Format is (from Gerber file format doc): // Creates the ProjectId. Format is (from Gerber file format doc):
// ProjectId,<project id>,<project GUID>,<revision id>*% // ProjectId,<project id>,<project GUID>,<revision id>*%
@ -334,7 +334,7 @@ void GERBER_JOBFILE_WRITER::addJSONGeneralSpecs()
void GERBER_JOBFILE_WRITER::addJSONFilesAttributes() void GERBER_JOBFILE_WRITER::addJSONFilesAttributes()
{ {
// Add the Files Attributes section in JSON format to m_JSONbuffer // Add the Files Attributes section in JSON format to m_JSONbuffer
m_json["FilesAttributes"] = json::array(); m_json["FilesAttributes"] = nlohmann::ordered_json::array();
for( unsigned ii = 0; ii < m_params.m_GerberFileList.GetCount(); ii++ ) for( unsigned ii = 0; ii < m_params.m_GerberFileList.GetCount(); ii++ )
{ {
@ -343,7 +343,8 @@ void GERBER_JOBFILE_WRITER::addJSONFilesAttributes()
wxString gbr_layer_id; wxString gbr_layer_id;
bool skip_file = false; // true to skip files which should not be in job file bool skip_file = false; // true to skip files which should not be in job file
const char* polarity = "Positive"; const char* polarity = "Positive";
json file_json;
nlohmann::ordered_json file_json;
if( layer <= B_Cu ) if( layer <= B_Cu )
{ {
@ -534,7 +535,7 @@ void GERBER_JOBFILE_WRITER::addJSONDesignRules()
if( hasInnerLayers ) if( hasInnerLayers )
{ {
m_json["DesignRules"] += json( { m_json["DesignRules"] += nlohmann::ordered_json( {
{ "Layers", "Inner" }, { "Layers", "Inner" },
{ "PadToPad", mapValue( minPadClearanceInner ) }, { "PadToPad", mapValue( minPadClearanceInner ) },
{ "PadToTrack", mapValue( minPadClearanceInner ) }, { "PadToTrack", mapValue( minPadClearanceInner ) },
@ -556,7 +557,7 @@ void GERBER_JOBFILE_WRITER::addJSONDesignRules()
void GERBER_JOBFILE_WRITER::addJSONMaterialStackup() void GERBER_JOBFILE_WRITER::addJSONMaterialStackup()
{ {
// Add the Material Stackup section in JSON format to m_JSONbuffer // Add the Material Stackup section in JSON format to m_JSONbuffer
m_json["MaterialStackup"] = json::array(); m_json["MaterialStackup"] = nlohmann::ordered_json::array();
// Build the candidates list: // Build the candidates list:
LSET maskLayer; LSET maskLayer;
@ -586,7 +587,8 @@ void GERBER_JOBFILE_WRITER::addJSONMaterialStackup()
double thickness = mapValue( item->GetThickness( sub_idx ) ); double thickness = mapValue( item->GetThickness( sub_idx ) );
wxString layer_type; wxString layer_type;
std::string layer_name; // for comment std::string layer_name; // for comment
json layer_json;
nlohmann::ordered_json layer_json;
switch( item->GetType() ) switch( item->GetType() )
{ {

View File

@ -30,9 +30,7 @@
#ifndef GERBER_JOBFILE_WRITER_H #ifndef GERBER_JOBFILE_WRITER_H
#define GERBER_JOBFILE_WRITER_H #define GERBER_JOBFILE_WRITER_H
#include <kicad_json.h> #include <nlohmann/json_fwd.hpp>
using json = kicad::json;
// A helper enum to handle sides of some layers (silk, mask) // A helper enum to handle sides of some layers (silk, mask)
@ -168,7 +166,7 @@ private:
REPORTER* m_reporter; // a reporter for messages (can be null) REPORTER* m_reporter; // a reporter for messages (can be null)
JOBFILE_PARAMS m_params; // the list of various prms and data to write in a job file JOBFILE_PARAMS m_params; // the list of various prms and data to write in a job file
double m_conversionUnits; // scaling factor to convert brd units to gerber units (mm) double m_conversionUnits; // scaling factor to convert brd units to gerber units (mm)
json m_json; // json document built by this class nlohmann::ordered_json m_json; // json document built by this class
}; };
#endif // #ifndef GERBER_JOBFILE_WRITER_H #endif // #ifndef GERBER_JOBFILE_WRITER_H