@@ -750,83 +750,45 @@ class boost_operator_impl : public any_impl
750750// ----------------------------------------------------------
751751
752752#ifdef BOOST_JSON_HAS_RAPIDJSON
753- struct rapidjson_crt_impl : public any_impl
753+ template <class Allocator , bool FullPrecision>
754+ struct rapidjson_impl : public any_impl
754755{
755- rapidjson_crt_impl (bool with_file_io)
756- : any_impl(" rapidjson" , false , false , with_file_io, parse_options() )
757- {}
758-
759- clock_type::duration
760- parse_string (file_item const & fi, std::size_t repeat) const override
761- {
762- using namespace rapidjson ;
763- auto const start = clock_type::now ();
764- while (repeat--)
765- {
766- CrtAllocator alloc;
767- GenericDocument<UTF8<>, CrtAllocator> d (&alloc);
768- d.Parse ( fi.text .data (), fi.text .size () );
769- }
770- return clock_type::now () - start;
771- }
772-
773- clock_type::duration
774- parse_file (file_item const & fi, std::size_t repeat) const override
775- {
776- using namespace rapidjson ;
777-
778- auto const start = clock_type::now ();
779- char * s = new char [ fi.text .size () ];
780- std::unique_ptr<char []> holder (s);
781-
782- while (repeat--)
783- {
784- FILE* f = fopen (fi.name .data (), " rb" );
785- std::size_t const sz = fread (s, 1 , fi.text .size (), f);
786-
787- CrtAllocator alloc;
788- GenericDocument<UTF8<>, CrtAllocator> d (&alloc);
789- d.Parse (s, sz);
790-
791- fclose (f);
792- }
793- return clock_type::now () - start;
794- }
795-
796- clock_type::duration
797- serialize_string (file_item const & fi, std::size_t repeat) const override
756+ static constexpr unsigned parse_flags
757+ = rapidjson::kParseDefaultFlags
758+ | (FullPrecision
759+ ? rapidjson::kParseFullPrecisionFlag
760+ : rapidjson::kParseNoFlags );
761+
762+ static
763+ parse_options
764+ make_parse_options () noexcept
798765 {
799- using namespace rapidjson ;
800- CrtAllocator alloc;
801- GenericDocument<UTF8<>, CrtAllocator> d (&alloc);
802- d.Parse ( fi.text .data (), fi.text .size () );
803-
804- auto const start = clock_type::now ();
805- rapidjson::StringBuffer st;
806- while (repeat--)
807- {
808- st.Clear ();
809- rapidjson::Writer<rapidjson::StringBuffer> wr (st);
810- d.Accept (wr);
811- }
812- return clock_type::now () - start;
766+ parse_options opts;
767+ opts.numbers = FullPrecision
768+ ? number_precision::precise : number_precision::imprecise;
769+ return opts;
813770 }
814- };
815771
816- struct rapidjson_memory_impl : public any_impl
817- {
818- rapidjson_memory_impl (bool with_file_io)
819- : any_impl(" rapidjson" , false , true , with_file_io, parse_options() )
772+ rapidjson_impl (bool with_file_io)
773+ : any_impl(
774+ " rapidjson" ,
775+ false ,
776+ std::is_same<Allocator, RAPIDJSON_DEFAULT_ALLOCATOR>::value,
777+ with_file_io,
778+ make_parse_options () )
820779 {}
821780
822781 clock_type::duration
823782 parse_string (file_item const & fi, std::size_t repeat) const override
824783 {
784+ using namespace rapidjson ;
825785 auto const start = clock_type::now ();
826786 while (repeat--)
827787 {
828- rapidjson::Document d;
829- d.Parse (fi.text .data (), fi.text .size ());
788+ Allocator alloc;
789+ GenericDocument<UTF8<>, Allocator> d (&alloc);
790+ d.template Parse <rapidjson_impl::parse_flags>(
791+ fi.text .data (), fi.text .size () );
830792 }
831793 return clock_type::now () - start;
832794 }
@@ -845,8 +807,9 @@ struct rapidjson_memory_impl : public any_impl
845807 FILE* f = fopen (fi.name .data (), " rb" );
846808 std::size_t const sz = fread (s, 1 , fi.text .size (), f);
847809
848- rapidjson::Document d;
849- d.Parse (s, sz);
810+ Allocator alloc;
811+ GenericDocument<UTF8<>, Allocator> d (&alloc);
812+ d.template Parse <rapidjson_impl::parse_flags>(s, sz);
850813
851814 fclose (f);
852815 }
@@ -856,8 +819,10 @@ struct rapidjson_memory_impl : public any_impl
856819 clock_type::duration
857820 serialize_string (file_item const & fi, std::size_t repeat) const override
858821 {
859- rapidjson::Document d;
860- d.Parse ( fi.text .data (), fi.text .size () );
822+ using namespace rapidjson ;
823+ Allocator alloc;
824+ GenericDocument<UTF8<>, Allocator> d (&alloc);
825+ d.template Parse <rapidjson_impl::parse_flags>( fi.text .data (), fi.text .size () );
861826
862827 auto const start = clock_type::now ();
863828 rapidjson::StringBuffer st;
@@ -1026,9 +991,25 @@ bool add_impl(impl_list & vi, char kind, char alloc, char io, char num)
1026991#ifdef BOOST_JSON_HAS_RAPIDJSON
1027992 case ' r' :
1028993 if (is_pool)
1029- impl = std::make_unique<rapidjson_memory_impl>(with_file_io);
994+ {
995+ using Allocator = RAPIDJSON_DEFAULT_ALLOCATOR;
996+ if (popts.numbers == number_precision::precise)
997+ impl = std::make_unique<rapidjson_impl<Allocator, true >>(
998+ with_file_io);
999+ else
1000+ impl = std::make_unique<rapidjson_impl<Allocator, false >>(
1001+ with_file_io);
1002+ }
10301003 else
1031- impl = std::make_unique<rapidjson_crt_impl>(with_file_io);
1004+ {
1005+ using Allocator = rapidjson::CrtAllocator;
1006+ if (popts.numbers == number_precision::precise)
1007+ impl = std::make_unique<rapidjson_impl<Allocator, true >>(
1008+ with_file_io);
1009+ else
1010+ impl = std::make_unique<rapidjson_impl<Allocator, false >>(
1011+ with_file_io);
1012+ }
10321013 break ;
10331014#endif // BOOST_JSON_HAS_RAPIDJSON
10341015
0 commit comments