Пример #1
0
    void test_array_of_strings()
    {
      fastjson::ArrayEntry array_entries[2];
      unsigned char buffer[] = "xxxxxxxxxxxx";
      fastjson::Document doc;
      doc.string_store = buffer;
      doc.array_store  = array_entries;
      bool ok = fastjson::parse_doc( "[\"hello\",\"world\"]" , &doc );
      saru_assert(ok);
      
      //We should have go the root element as the array
      saru_assert( doc.root.type == fastjson::Token::ArrayToken );
      saru_assert_equal( &array_entries[0], doc.root.array.ptr );

      //Now we should have got two strings in the array
      saru_assert( array_entries[0].tok.type == fastjson::Token::ValueToken );
      saru_assert_equal( 5u, array_entries[0].tok.value.size );
      saru_assert( buffer == reinterpret_cast<unsigned char*>(array_entries[0].tok.value.ptr) ); 
      saru_assert_equal( &array_entries[1], array_entries[0].next );

      saru_assert( array_entries[1].tok.type == fastjson::Token::ValueToken );
      saru_assert_equal( 5u, array_entries[1].tok.value.size );
      saru_assert( buffer+5 == reinterpret_cast<unsigned char*>(array_entries[1].tok.value.ptr) ); 
      saru_assert_equal( (void*)NULL, array_entries[1].next );
    }
    void read_tab()
    {
      std::string in_str("\"xx\\txx\"");

      saru_assert( fastjson::dom::parse_string(in_str, &token, &chunk, 0, &ErrorGetter::on_error, &error_getter ) );
      saru_assert_equal( fastjson::Token::ValueToken, token.type );
      saru_assert_equal( 5u, token.value.size );
      saru_assert( token.value.ptr );
      saru_assert_equal( "xx\txx", std::string( token.value.ptr, token.value.size ) );
    }
Пример #3
0
 void test_number_root()
 {
   unsigned char buffer[] = "xxxxxxxxxxxx";
   fastjson::Document doc;
   doc.string_store = buffer;
   bool ok = fastjson::parse_doc( "123", &doc );
   saru_assert( ok );
   
   //Now we should have a root array element with no children.
   saru_assert( doc.root.type == fastjson::Token::ValueToken );
   saru_assert( doc.root.value.ptr == reinterpret_cast<char*>(buffer) ); 
   saru_assert_equal(3u, doc.root.value.size);
   saru_assert_equal( fastjson::ValueType::NumberHint, doc.root.value.type_hint);
 }
    void read_tab()
    {
      std::string in_str("\"xx\\txx\"");
      fastjson::dom::Chunk chunk;
      fastjson::Token token;

      std::string error_message;

      saru_assert( fastjson::dom::parse_string(in_str, &token, &chunk, &error_message ) );
      saru_assert_equal( fastjson::Token::ValueToken, token.type );
      saru_assert_equal( 5u, token.data.value.size );
      saru_assert( token.data.value.ptr );
      saru_assert_equal( "xx\txx", std::string( token.data.value.ptr, token.data.value.size ) );
    }
    void round_trip_tab()
    {
      std::string in_str("\"xx\\txx\"");

      saru_assert( fastjson::dom::parse_string(in_str, &token, &chunk, 0, &ErrorGetter::on_error, &error_getter ) );
      saru_assert_equal( in_str, fastjson::as_string( &token ) ); 
    }
Пример #6
0
 void test_create_from_string_bad()
 {
   std::string json("[");
   saru_assert( ! fastjson::dom::parse_string(json, &token, &chunk, 0, &ErrorGetter::on_error, &error_getter ) );
   saru_assert( error_getter.ec );
   saru_assert_equal("Input ended while in non-root state", error_getter.ec->mesg );
 }
Пример #7
0
 void test_create_from_string_mega()
 {
   std::string json("{\"hello\":[\"world\",123,4.5],\"say\":{\"moo\":\"cow\",\"eep\":null},\"say\":{\"moo\":\"cow\",\"eep\":null},\"say2\":{\"moo\":\"cow\",\"eep\":null},\"say3\":{\"moo\":\"cow\",\"eep\":null},\"say4\":{\"moo\":\"cow\",\"eep\":null},\"say5\":{\"moo\":\"cow\",\"eep\":null},\"say6\":{\"moo\":\"cow\",\"eep\":null}}");
   saru_assert( fastjson::dom::parse_string(json, &token, &chunk, 0, &ErrorGetter::on_error, &error_getter ) );
   saru_assert( ! error_getter.ec );
   saru_assert( token.type == fastjson::Token::DictToken );
   saru_assert_equal( json, fastjson::as_string( &token ) ); 
 }
Пример #8
0
    void naked_literal_false()
    {
      fastjson::Document doc;
      bool ok = fastjson::parse_doc( "false" , &doc );
      saru_assert(ok);

      saru_assert_equal( fastjson::Token::LiteralFalseToken, doc.root.type );
    }
Пример #9
0
 void test_unicode_3byte()
 {
   unsigned char buffer[] = "xxxxxxx";
   fastjson::Document doc;
   doc.string_store = buffer+1;
   bool ok = fastjson::parse_doc( "\"z\\u20ACz\"" , &doc );
   saru_assert_equal( fastjson::Token::ValueToken, doc.root.type );
   saru_assert_equal( 5u, doc.root.value.size );
   saru_assert_equal( (void*)(buffer+1), doc.root.value.ptr );
   saru_assert_equal( 'x', *buffer );
   saru_assert_equal( 'z', *(buffer+1) );
   saru_assert_equal( 0xE2, *(buffer+2) );
   saru_assert_equal( 0x82, *(buffer+3) );
   saru_assert_equal( 0xAC, *(buffer+4) );
   saru_assert_equal( 'z', *(buffer+5) );
   saru_assert_equal( 'x', *(buffer+6) );
 }
Пример #10
0
    void naked_literal_null()
    {
      fastjson::Document doc;
      bool ok = fastjson::parse_doc( "null" , &doc );
      saru_assert(ok);

      saru_assert_equal( fastjson::Token::LiteralNullToken, doc.root.type );
    }
    void write_tab()
    {
      char * buffer = "xx\txx";
      token.type = fastjson::Token::ValueToken;
      token.value.type_hint = fastjson::ValueType::StringHint;
      token.value.size = 5;
      token.value.ptr = buffer;

      saru_assert_equal( "\"xx\\txx\"", fastjson::as_string( &token ) ); 
    }
    void round_trip_tab()
    {
      std::string in_str("\"xx\\txx\"");
      fastjson::dom::Chunk chunk;
      fastjson::Token token;

      std::string error_message;

      saru_assert( fastjson::dom::parse_string(in_str, &token, &chunk, &error_message ) );
      saru_assert_equal( in_str, fastjson::as_string( &token ) ); 
    }
    void test_get()
    {
      fastjson::dom::Chunk chunk;
      fastjson::Token token;
      saru_assert( fastjson::dom::parse_string("{\"key\":\"value\"}", &token, &chunk, 0, NULL, NULL ) );

      fastjson::dom::Dictionary_const dict = fastjson::dom::Dictionary_const::as_dict(&token);

      std::string value;
      saru_assert( dict.get<std::string>("key", &value ) );
      saru_assert_equal("value", value );
    }
Пример #14
0
    void test_string_buffer()
    {
      fastjson::ArrayEntry array_entries[2];
      unsigned char buffer[] = "xxxxxxxxxxxx"; 
      fastjson::Document doc;
      doc.string_store = buffer+1;
      doc.array_store = array_entries;

      bool ok = fastjson::parse_doc( "[\"hello\",\"world\"]", & doc );
      saru_assert(ok);

      saru_assert_equal( std::string("xhelloworldx"), std::string( reinterpret_cast<const char*>(buffer) ) );
    }
Пример #15
0
    void test_number_buffer()
    {
      fastjson::ArrayEntry array_entries[2];
      unsigned char buffer[] = "xxxxxxxxxxxx";
      fastjson::Document doc;
      doc.string_store = buffer+1;
      doc.array_store = array_entries;

      bool ok = fastjson::parse_doc( "[12345,67890]", & doc );
      saru_assert(ok);

      saru_assert_equal( std::string("x1234567890x"), std::string( reinterpret_cast<const char*>(buffer) ) );
    }
  void number_as_key_bad2()
  {
        fastjson::JsonElementCount jse;

        ErrorHelper eh;
        jse.user_error_callback = &ErrorHelper::on_error;
        jse.user_data = &eh;
        jse.mode = 0;

        bool ok = fastjson::count_elements( "{\"a\":\"y\",2:\"y\"}" , &jse );

        saru_assert(!ok);
        saru_assert( eh.ec_ );
        saru_assert_equal("Unexpected character when looking for dict key", eh.ec_->mesg );
  }
  void number_as_key_bad()
  {
        fastjson::JsonElementCount jse;

        ErrorHelper eh;
        jse.user_error_callback = &ErrorHelper::on_error;
        jse.user_data = &eh;
        jse.mode = 0;

        bool ok = fastjson::count_elements( "{2:\"y\"}" , &jse );

        saru_assert(!ok);
        saru_assert( eh.ec_ );
        saru_assert_equal("Unexpected character while parsing dict start", eh.ec_->mesg );
  }
Пример #18
0
 void test_unicode_2byte()
 {
   unsigned char buffer[] = "xxxxxxx";
   fastjson::Document doc;
   doc.string_store = buffer+1;
   bool ok = fastjson::parse_doc( "\"\\u00A2\"" , &doc );
   saru_assert_equal( fastjson::Token::ValueToken, doc.root.type );
   saru_assert_equal( 2u, doc.root.value.size );
   saru_assert_equal( (void*)(buffer+1), doc.root.value.ptr );
   saru_assert_equal( 'x', *buffer );
   saru_assert_equal( 0xC2, *(buffer+1) );
   saru_assert_equal( 0xA2, *(buffer+2) );
   saru_assert_equal( 'x', *(buffer+3) );
 }
Пример #19
0
    void test_complex_dict()
    {
        fastjson::ArrayEntry array_entries[4];
        fastjson::DictEntry  dict_entries[2];
        unsigned char buffer[] = "xxxxxxx";
        fastjson::Document doc;
        doc.string_store = buffer;
        doc.array_store = array_entries;
        doc.dict_store  = dict_entries;

        bool ok = fastjson::parse_doc( "{\"f\":[\"g\",\"h\"],\"i\":[\"j\",\"k\"]}" , &doc );
        saru_assert(ok);

        //We should have go the root element as the array
        saru_assert_equal( fastjson::Token::DictToken, doc.root.type );
        saru_assert_equal( &dict_entries[0], doc.root.dict.ptr );

        //The first entry in the dictionary
        saru_assert_equal( &dict_entries[1],            dict_entries[0].next );
        //  First Key
        saru_assert_equal( fastjson::Token::ValueToken, dict_entries[0].key_tok.type );
        saru_assert_equal( fastjson::ValueType::StringHint,  dict_entries[0].key_tok.value.type_hint );
        saru_assert_equal( 1u,                          dict_entries[0].key_tok.value.size );
        saru_assert_equal( (void*)buffer,               (void*)dict_entries[0].key_tok.value.ptr );
        saru_assert_equal( 'f',                         buffer[0] );
        //  First Value
        saru_assert_equal( fastjson::Token::ArrayToken, dict_entries[0].value_tok.type );
        saru_assert_equal( array_entries,               dict_entries[0].value_tok.array.ptr );
        
        //The second entry in the dictionary
        saru_assert_equal( (void*)NULL,                 dict_entries[1].next);
        //  Second Key
        saru_assert_equal( fastjson::Token::ValueToken, dict_entries[1].key_tok.type );
        saru_assert_equal( fastjson::ValueType::StringHint,  dict_entries[1].key_tok.value.type_hint );
        saru_assert_equal( 1u,                          dict_entries[1].key_tok.value.size );
        saru_assert_equal( (void*)(buffer+3),           (void*)dict_entries[1].key_tok.value.ptr );
        saru_assert_equal( 'i',                         buffer[3] );

        //  Second Value
        saru_assert_equal( fastjson::Token::ArrayToken, dict_entries[1].value_tok.type );
        saru_assert_equal( array_entries+2,             dict_entries[1].value_tok.array.ptr );

        //Check the array entries.
        saru_assert_equal( array_entries+1,                 array_entries[0].next );
        saru_assert_equal( fastjson::Token::ValueToken,     array_entries[0].tok.type );
        saru_assert_equal( fastjson::ValueType::StringHint, array_entries[0].tok.value.type_hint );
        saru_assert_equal( 1u,                              array_entries[0].tok.value.size );
        saru_assert_equal( (void*)(buffer+1),        (void*)array_entries[0].tok.value.ptr );
        saru_assert_equal( 'g',                             buffer[1] );

        saru_assert_equal( (void*)0,                        array_entries[1].next );
        saru_assert_equal( fastjson::Token::ValueToken,     array_entries[1].tok.type );
        saru_assert_equal( fastjson::ValueType::StringHint, array_entries[1].tok.value.type_hint );
        saru_assert_equal( 1u,                              array_entries[1].tok.value.size );
        saru_assert_equal( (void*)(buffer+2),        (void*)array_entries[1].tok.value.ptr );
        saru_assert_equal( 'h',                             buffer[2] );

        saru_assert_equal( array_entries+3,                 array_entries[2].next );
        saru_assert_equal( fastjson::Token::ValueToken,     array_entries[2].tok.type );
        saru_assert_equal( fastjson::ValueType::StringHint, array_entries[2].tok.value.type_hint );
        saru_assert_equal( 1u,                              array_entries[2].tok.value.size );
        saru_assert_equal( (void*)(buffer+4),        (void*)array_entries[2].tok.value.ptr );
        saru_assert_equal( 'j',                             buffer[4] );

        saru_assert_equal( (void*)0,                        array_entries[3].next );
        saru_assert_equal( fastjson::Token::ValueToken,     array_entries[3].tok.type );
        saru_assert_equal( fastjson::ValueType::StringHint, array_entries[3].tok.value.type_hint );
        saru_assert_equal( 1u,                              array_entries[3].tok.value.size );
        saru_assert_equal( (void*)(buffer+5),        (void*)array_entries[3].tok.value.ptr );
        saru_assert_equal( 'k',                             buffer[5] );

    }