From 38ca0b9d645cabba7a481ebf6e16ad4279dbf01f Mon Sep 17 00:00:00 2001 From: Russell Standish Date: Thu, 2 Dec 2021 09:54:42 +1100 Subject: [PATCH 1/3] Handle NaNs on input Correctly display real special values on output Remove deprecation warning on boost placeholders --- json5_parser/json5_parser_reader_template.h | 12 +++++++++++- json5_parser/json5_parser_writer_template.h | 5 +++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/json5_parser/json5_parser_reader_template.h b/json5_parser/json5_parser_reader_template.h index 557035b..984edb4 100644 --- a/json5_parser/json5_parser_reader_template.h +++ b/json5_parser/json5_parser_reader_template.h @@ -15,7 +15,7 @@ #define BOOST_SPIRIT_THREADSAFE // uncomment for multithreaded use, requires linking to boost.thread -#include +#include #include #include @@ -37,6 +37,7 @@ namespace json5_parser { + using namespace boost::placeholders; const spirit_namespace::int_parser < boost::int64_t > int64_p = spirit_namespace::int_parser < boost::int64_t >(); const spirit_namespace::uint_parser< boost::uint64_t > uint64_p = spirit_namespace::uint_parser< boost::uint64_t >(); @@ -312,6 +313,13 @@ namespace json5_parser add_to_current( -std::numeric_limits::infinity() ); } + + void new_nan( Iter_type begin, Iter_type end ) + { + assert( is_eq( begin, end, "NaN" ) ); + + add_to_current( -std::numeric_limits::quiet_NaN() ); + } private: @@ -474,6 +482,7 @@ namespace json5_parser Real_action new_real ( boost::bind( &Semantic_actions_t::new_real, &self.actions_, _1 ) ); Str_action new_infinity ( boost::bind( &Semantic_actions_t::new_infinity, &self.actions_, _1, _2 ) ); Str_action new_minus_infinity ( boost::bind( &Semantic_actions_t::new_minus_infinity, &self.actions_, _1, _2 ) ); + Str_action new_nan ( boost::bind( &Semantic_actions_t::new_nan, &self.actions_, _1, _2 ) ); Int_action new_int ( boost::bind( &Semantic_actions_t::new_int, &self.actions_, _1 ) ); Uint64_action new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64, &self.actions_, _1 ) ); @@ -494,6 +503,7 @@ namespace json5_parser | str_p( "null" ) [ new_null ] | (!ch_p('+') >> str_p( "Infinity" ) [ new_infinity ]) | str_p( "-Infinity" ) [ new_minus_infinity ] + | str_p( "NaN" ) [ new_nan ] ; object_ diff --git a/json5_parser/json5_parser_writer_template.h b/json5_parser/json5_parser_writer_template.h index b9ab62e..37d8e66 100644 --- a/json5_parser/json5_parser_writer_template.h +++ b/json5_parser/json5_parser_writer_template.h @@ -190,7 +190,12 @@ namespace json5_parser void output( double d ) { + if (std::isfinite(d)) os_ << std::setprecision( precision_of_doubles_ ) << d; + else if (std::isnan(d)) + os_<<"NaN"; + else // is infinite + os_<<(d<0?"-":"")<<"Infinity"; } static bool contains_composite_elements( const Array_type& arr ) From 918f87bd3994395e0e43cdb86e5994ab2fe61747 Mon Sep 17 00:00:00 2001 From: Russell Standish Date: Thu, 15 Dec 2022 15:20:34 +1100 Subject: [PATCH 2/3] Added single quote processing of object keys. --- json5_parser/json5_parser_reader_template.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json5_parser/json5_parser_reader_template.h b/json5_parser/json5_parser_reader_template.h index 984edb4..4f70049 100644 --- a/json5_parser/json5_parser_reader_template.h +++ b/json5_parser/json5_parser_reader_template.h @@ -517,7 +517,7 @@ namespace json5_parser ; pair_ - = (double_quoted_string_[ new_name ] | identifier_[ new_identifier ]) + = (single_quoted_string_[ new_name ] | double_quoted_string_[ new_name ] | identifier_[ new_identifier ]) >> ( ':' | eps_p[ &throw_not_colon ] ) >> ( value_ | eps_p[ &throw_not_value ] ) ; From b095e5feb689fbb93beb4cc767eb00670152a614 Mon Sep 17 00:00:00 2001 From: Russell Standish Date: Tue, 20 Aug 2024 18:34:14 +1000 Subject: [PATCH 3/3] ifdef'd out BOOST_SPIRIT_THREADSAFE when compiling with emscripten --- json5_parser/json5_parser_reader_template.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/json5_parser/json5_parser_reader_template.h b/json5_parser/json5_parser_reader_template.h index 984edb4..298bac0 100644 --- a/json5_parser/json5_parser_reader_template.h +++ b/json5_parser/json5_parser_reader_template.h @@ -13,7 +13,10 @@ #include "json5_parser_value.h" #include "json5_parser_error_position.h" +// emscripten threads are complicated to use and experimental +#ifndef __EMSCRIPTEN__ #define BOOST_SPIRIT_THREADSAFE // uncomment for multithreaded use, requires linking to boost.thread +#endif #include #include