1919#ifndef JSON_TEST_PATH
2020#define JSON_TEST_PATH " jsonexamples/twitter.json"
2121#endif
22+ #ifndef NDJSON_TEST_PATH
23+ #define NDJSON_TEST_PATH " jsonexamples/amazon_cellphones.ndjson"
24+ #endif
2225
2326// ulp distance
2427// Marc B. Reynolds, 2016-2019
@@ -745,6 +748,154 @@ bool skyprophet_test() {
745748 return true ;
746749}
747750
751+ namespace parse_api {
752+ using namespace std ;
753+ using namespace simdjson ;
754+
755+ const padded_string BASIC_JSON = string(" [1,2,3]" );
756+ const padded_string BASIC_NDJSON = string(" [1,2,3]\n [4,5,6]" );
757+ // const padded_string EMPTY_NDJSON = string("");
758+
759+ bool document_parse () {
760+ std::cout << " Running " << __func__ << std::endl;
761+ auto [doc, error] = document::parse (BASIC_JSON );
762+ if (error) { cerr << error << endl; return false ; }
763+ if (!doc.root ().is_array ()) { cerr << " Document did not parse as an array" << endl; return false ; }
764+ return true ;
765+ }
766+ bool parser_parse () {
767+ std::cout << " Running " << __func__ << std::endl;
768+ document::parser parser;
769+ auto [doc, error] = parser.parse (BASIC_JSON );
770+ if (error) { cerr << error << endl; return false ; }
771+ if (!doc.root ().is_array ()) { cerr << " Document did not parse as an array" << endl; return false ; }
772+ return true ;
773+ }
774+ bool parser_parse_many () {
775+ std::cout << " Running " << __func__ << std::endl;
776+ document::parser parser;
777+ int count = 0 ;
778+ for (auto [doc, error] : parser.parse_many (BASIC_NDJSON )) {
779+ if (error) { cerr << error << endl; return false ; }
780+ if (!doc.root ().is_array ()) { cerr << " Document did not parse as an array" << endl; return false ; }
781+ count++;
782+ }
783+ if (count != 2 ) { cerr << " parse_many returned " << count << " documents, expected 2" << endl; return false ; }
784+ return true ;
785+ }
786+ // bool parser_parse_many_empty() {
787+ // std::cout << "Running " << __func__ << std::endl;
788+ // document::parser parser;
789+ // int count = 0;
790+ // for (auto [doc, error] : parser.parse_many(EMPTY_NDJSON)) {
791+ // if (error) { cerr << error << endl; return false; }
792+ // count++;
793+ // }
794+ // if (count != 0) { cerr << "parse_many returned " << count << " documents, expected 0" << endl; return false; }
795+ // return true;
796+ // }
797+
798+ bool document_load () {
799+ std::cout << " Running " << __func__ << std::endl;
800+ auto [doc, error] = document::load (JSON_TEST_PATH );
801+ if (error) { cerr << error << endl; return false ; }
802+ if (!doc.root ().is_object ()) { cerr << " Document did not parse as an object" << endl; return false ; }
803+ return true ;
804+ }
805+ bool parser_load () {
806+ std::cout << " Running " << __func__ << std::endl;
807+ document::parser parser;
808+ auto [doc, error] = parser.load (JSON_TEST_PATH );
809+ if (error) { cerr << error << endl; return false ; }
810+ if (!doc.root ().is_object ()) { cerr << " Document did not parse as an object" << endl; return false ; }
811+ return true ;
812+ }
813+ bool parser_load_many () {
814+ std::cout << " Running " << __func__ << std::endl;
815+ document::parser parser;
816+ int count = 0 ;
817+ for (auto [doc, error] : parser.load_many (NDJSON_TEST_PATH )) {
818+ if (error) { cerr << error << endl; return false ; }
819+ if (!doc.root ().is_array ()) { cerr << " Document did not parse as an array" << endl; return false ; }
820+ count++;
821+ }
822+ if (count != 793 ) { cerr << " Expected 793 documents, but load_many loaded " << count << " documents." << endl; return false ; }
823+ return true ;
824+ }
825+
826+ #if SIMDJSON_EXCEPTIONS
827+
828+ bool document_parse_exception () {
829+ std::cout << " Running " << __func__ << std::endl;
830+ document doc = document::parse (BASIC_JSON );
831+ if (!doc.root ().is_array ()) { cerr << " Document did not parse as an array" << endl; return false ; }
832+ return true ;
833+ }
834+ bool parser_parse_exception () {
835+ std::cout << " Running " << __func__ << std::endl;
836+ document::parser parser;
837+ const document& doc = parser.parse (BASIC_JSON );
838+ if (!doc.root ().is_array ()) { cerr << " Document did not parse as an array" << endl; return false ; }
839+ return true ;
840+ }
841+ bool parser_parse_many_exception () {
842+ std::cout << " Running " << __func__ << std::endl;
843+ document::parser parser;
844+ int count = 0 ;
845+ for (const document &doc : parser.parse_many (BASIC_NDJSON )) {
846+ if (!doc.root ().is_array ()) { cerr << " Document did not parse as an array" << endl; return false ; }
847+ count++;
848+ }
849+ if (count != 2 ) { cerr << " parse_many returned " << count << " documents, expected 2" << endl; return false ; }
850+ return true ;
851+ }
852+
853+ bool document_load_exception () {
854+ std::cout << " Running " << __func__ << std::endl;
855+ document doc = document::load (JSON_TEST_PATH );
856+ if (!doc.root ().is_object ()) { cerr << " Document did not parse as an object" << endl; return false ; }
857+ return true ;
858+ }
859+ bool parser_load_exception () {
860+ std::cout << " Running " << __func__ << std::endl;
861+ document::parser parser;
862+ const document &doc = parser.load (JSON_TEST_PATH );
863+ if (!doc.root ().is_object ()) { cerr << " Document did not parse as an object" << endl; return false ; }
864+ return true ;
865+ }
866+ bool parser_load_many_exception () {
867+ std::cout << " Running " << __func__ << std::endl;
868+ document::parser parser;
869+ int count = 0 ;
870+ for (const document &doc : parser.load_many (NDJSON_TEST_PATH )) {
871+ if (!doc.root ().is_array ()) { cerr << " Document did not parse as an array" << endl; return false ; }
872+ count++;
873+ }
874+ if (count != 793 ) { cerr << " Expected 1 document, but load_many loaded " << count << " documents." << endl; return false ; }
875+ return true ;
876+ }
877+ #endif
878+
879+ bool run_tests () {
880+ return document_parse () &&
881+ parser_parse () &&
882+ parser_parse_many () &&
883+ // parser_parse_many_empty() &&
884+ document_load () &&
885+ parser_load () &&
886+ parser_load_many () &&
887+ #if SIMDJSON_EXCEPTIONS
888+ document_parse_exception () &&
889+ parser_parse_exception () &&
890+ parser_parse_many_exception () &&
891+ document_load_exception () &&
892+ parser_load_exception () &&
893+ parser_load_many_exception () &&
894+ #endif
895+ true ;
896+ }
897+ }
898+
748899namespace dom_api {
749900 using namespace std ;
750901 using namespace simdjson ;
@@ -892,8 +1043,7 @@ namespace dom_api {
8921043 if (doc[" a" ].as_uint64_t ().first != 1 ) { cerr << " Expected uint64_t(doc[\" a\" ]) to be 1, was " << doc[" a" ].first << endl; return false ; }
8931044
8941045 UNUSED document::element val;
895- // tie(val, error) = doc["d"]; fails with "no viable overloaded '='" on Apple clang version 11.0.0
896- doc[" d" ].tie (val, error);
1046+ tie (val, error) = doc[" d" ];
8971047 if (error != simdjson::NO_SUCH_FIELD ) { cerr << " Expected NO_SUCH_FIELD error for uint64_t(doc[\" d\" ]), got " << error << endl; return false ; }
8981048 return true ;
8991049 }
@@ -907,11 +1057,11 @@ namespace dom_api {
9071057 if (doc[" obj" ][" a" ].as_uint64_t ().first != 1 ) { cerr << " Expected uint64_t(doc[\" obj\" ][\" a\" ]) to be 1, was " << doc[" obj" ][" a" ].first << endl; return false ; }
9081058
9091059 document::object obj;
910- doc. as_object (). tie (obj, error); // tie(...) = fails with "no viable overloaded '='" on Apple clang version 11.0.0
1060+ tie (obj, error) = doc. as_object ();
9111061 if (error) { cerr << " Error: " << error << endl; return false ; }
9121062 if (obj[" obj" ][" a" ].as_uint64_t ().first != 1 ) { cerr << " Expected uint64_t(doc[\" obj\" ][\" a\" ]) to be 1, was " << doc[" obj" ][" a" ].first << endl; return false ; }
9131063
914- obj[ " obj " ]. as_object (). tie (obj, error); // tie(...) = fails with "no viable overloaded '='" on Apple clang version 11.0.0
1064+ tie (obj, error) = obj[ " obj " ]. as_object ();
9151065 if (obj[" a" ].as_uint64_t ().first != 1 ) { cerr << " Expected uint64_t(obj[\" a\" ]) to be 1, was " << obj[" a" ].first << endl; return false ; }
9161066 if (obj[" b" ].as_uint64_t ().first != 2 ) { cerr << " Expected uint64_t(obj[\" b\" ]) to be 2, was " << obj[" b" ].first << endl; return false ; }
9171067 if (obj[" c" ].as_uint64_t ().first != 3 ) { cerr << " Expected uint64_t(obj[\" c\" ]) to be 3, was " << obj[" c" ].first << endl; return false ; }
@@ -921,7 +1071,7 @@ namespace dom_api {
9211071 if (obj[" a" ].as_uint64_t ().first != 1 ) { cerr << " Expected uint64_t(obj[\" a\" ]) to be 1, was " << obj[" a" ].first << endl; return false ; }
9221072
9231073 UNUSED document::element val;
924- doc[ " d " ]. tie (val, error); // tie(...) = fails with "no viable overloaded '='" on Apple clang version 11.0.0
1074+ tie (val, error) = doc[ " d " ];
9251075 if (error != simdjson::NO_SUCH_FIELD ) { cerr << " Expected NO_SUCH_FIELD error for uint64_t(obj[\" d\" ]), got " << error << endl; return false ; }
9261076 return true ;
9271077 }
@@ -945,14 +1095,14 @@ namespace dom_api {
9451095 if (error) { cerr << " Error: " << error << endl; return false ; }
9461096 for (auto tweet : tweets) {
9471097 document::object user;
948- tweet[ " user " ]. as_object (). tie (user, error); // tie(...) = fails with "no viable overloaded '='" on Apple clang version 11.0.0
1098+ tie (user, error) = tweet[ " user " ]. as_object ();
9491099 if (error) { cerr << " Error: " << error << endl; return false ; }
9501100 bool default_profile;
951- user[ " default_profile " ]. as_bool (). tie (default_profile, error); // tie(...) = fails with "no viable overloaded '='" on Apple clang version 11.0.0
1101+ tie (default_profile, error) = user[ " default_profile " ]. as_bool ();
9521102 if (error) { cerr << " Error: " << error << endl; return false ; }
9531103 if (default_profile) {
9541104 std::string_view screen_name;
955- user[ " screen_name " ]. as_string (). tie (screen_name, error); // tie(...) = fails with "no viable overloaded '='" on Apple clang version 11.0.0
1105+ tie (screen_name, error) = user[ " screen_name " ]. as_string ();
9561106 if (error) { cerr << " Error: " << error << endl; return false ; }
9571107 default_users.insert (screen_name);
9581108 }
@@ -973,13 +1123,13 @@ namespace dom_api {
9731123 if (!not_found) {
9741124 for (auto image : media) {
9751125 document::object sizes;
976- image[ " sizes " ]. as_object (). tie (sizes, error); // tie(...) = fails with "no viable overloaded '='" on Apple clang version 11.0.0
1126+ tie (sizes, error) = image[ " sizes " ]. as_object ();
9771127 if (error) { cerr << " Error: " << error << endl; return false ; }
9781128 for (auto [key, size] : sizes) {
9791129 uint64_t width, height;
980- size[ " w " ]. as_uint64_t (). tie (width, error); // tie(...) = fails with "no viable overloaded '='" on Apple clang version 11.0.0
1130+ tie (width, error) = size[ " w " ]. as_uint64_t ();
9811131 if (error) { cerr << " Error: " << error << endl; return false ; }
982- size[ " h " ]. as_uint64_t (). tie (height, error); // tie(...) = fails with "no viable overloaded '='" on Apple clang version 11.0.0
1132+ tie (height, error) = size[ " h " ]. as_uint64_t ();
9831133 if (error) { cerr << " Error: " << error << endl; return false ; }
9841134 image_sizes.insert (make_pair (width, height));
9851135 }
@@ -1508,6 +1658,8 @@ int main(int argc, char *argv[]) {
15081658 return EXIT_FAILURE ;
15091659 if (!skyprophet_test ())
15101660 return EXIT_FAILURE ;
1661+ if (!parse_api::run_tests ())
1662+ return EXIT_FAILURE ;
15111663 if (!dom_api::run_tests ())
15121664 return EXIT_FAILURE ;
15131665 if (!format_tests::run_tests ())
0 commit comments