KiCad PCB EDA Suite
Loading...
Searching...
No Matches
json_schema_validator.cpp
Go to the documentation of this file.
1/*
2* This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <fstream>
21#include <wx/filename.h>
22#include <wx/log.h>
23
25#include <locale_io.h>
26
27
28JSON_SCHEMA_VALIDATOR::JSON_SCHEMA_VALIDATOR( const wxFileName& aSchemaFile )
29{
30 std::ifstream schema_stream( aSchemaFile.GetFullPath().fn_str() );
31 nlohmann::json schema;
32
33 try
34 {
35 schema_stream >> schema;
36 setRootSchema( schema );
37 }
38 catch( std::exception& e )
39 {
40 if( !aSchemaFile.FileExists() )
41 {
42 wxLogError( wxString::Format( _( "schema file '%s' not found" ),
43 aSchemaFile.GetFullPath() ) );
44 }
45 else
46 {
47 wxLogError( wxString::Format( _( "Error loading schema: %s" ), e.what() ) );
48 }
49 }
50}
51
52
53JSON_SCHEMA_VALIDATOR::JSON_SCHEMA_VALIDATOR( const nlohmann::json& aSchema )
54{
55 try
56 {
57 setRootSchema( aSchema );
58 }
59 catch( std::exception& e )
60 {
61 wxLogError( wxString::Format( _( "Error loading schema: %s" ), e.what() ) );
62 }
63}
64
65
66void JSON_SCHEMA_VALIDATOR::setRootSchema( const nlohmann::json& aSchema )
67{
68 // For some obscure reason on MINGW, using UCRT option,
69 // m_schema_validator.set_root_schema() hangs without switching to locale "C"
70#if defined(__MINGW32__) && defined(_UCRT)
72#endif
73
74 m_validator.set_root_schema( aSchema );
75}
76
77
78nlohmann::json JSON_SCHEMA_VALIDATOR::Validate( const nlohmann::json& aJson,
79 nlohmann::json_schema::error_handler& aErrorHandler,
80 const nlohmann::json_uri& aInitialUri ) const
81{
82 return m_validator.validate( aJson, aErrorHandler, aInitialUri );
83}
void setRootSchema(const nlohmann::json &aSchema)
JSON_SCHEMA_VALIDATOR(const wxFileName &aSchemaFile)
nlohmann::json_schema::json_validator m_validator
nlohmann::json Validate(const nlohmann::json &aJson, nlohmann::json_schema::error_handler &aErrorHandler, const nlohmann::json_uri &aInitialUri=nlohmann::json_uri("#")) const
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
#define _(s)
std::vector< FAB_LAYER_COLOR > dummy