Home | History | Annotate | Download | only in json
      1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/json/json_writer.h"
      6 #include "base/values.h"
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 namespace base {
     10 
     11 TEST(JSONWriterTest, Writing) {
     12   // Test null
     13   Value* root = Value::CreateNullValue();
     14   std::string output_js;
     15   JSONWriter::Write(root, false, &output_js);
     16   ASSERT_EQ("null", output_js);
     17   delete root;
     18 
     19   // Test empty dict
     20   root = new DictionaryValue;
     21   JSONWriter::Write(root, false, &output_js);
     22   ASSERT_EQ("{}", output_js);
     23   delete root;
     24 
     25   // Test empty list
     26   root = new ListValue;
     27   JSONWriter::Write(root, false, &output_js);
     28   ASSERT_EQ("[]", output_js);
     29   delete root;
     30 
     31   // Test Real values should always have a decimal or an 'e'.
     32   root = Value::CreateDoubleValue(1.0);
     33   JSONWriter::Write(root, false, &output_js);
     34   ASSERT_EQ("1.0", output_js);
     35   delete root;
     36 
     37   // Test Real values in the the range (-1, 1) must have leading zeros
     38   root = Value::CreateDoubleValue(0.2);
     39   JSONWriter::Write(root, false, &output_js);
     40   ASSERT_EQ("0.2", output_js);
     41   delete root;
     42 
     43   // Test Real values in the the range (-1, 1) must have leading zeros
     44   root = Value::CreateDoubleValue(-0.8);
     45   JSONWriter::Write(root, false, &output_js);
     46   ASSERT_EQ("-0.8", output_js);
     47   delete root;
     48 
     49   // Writer unittests like empty list/dict nesting,
     50   // list list nesting, etc.
     51   DictionaryValue root_dict;
     52   ListValue* list = new ListValue;
     53   root_dict.Set("list", list);
     54   DictionaryValue* inner_dict = new DictionaryValue;
     55   list->Append(inner_dict);
     56   inner_dict->SetInteger("inner int", 10);
     57   ListValue* inner_list = new ListValue;
     58   list->Append(inner_list);
     59   list->Append(Value::CreateBooleanValue(true));
     60 
     61   // Test the pretty-printer.
     62   JSONWriter::Write(&root_dict, false, &output_js);
     63   ASSERT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js);
     64   JSONWriter::Write(&root_dict, true, &output_js);
     65   // The pretty-printer uses a different newline style on Windows than on
     66   // other platforms.
     67 #if defined(OS_WIN)
     68 #define JSON_NEWLINE "\r\n"
     69 #else
     70 #define JSON_NEWLINE "\n"
     71 #endif
     72   ASSERT_EQ("{" JSON_NEWLINE
     73             "   \"list\": [ {" JSON_NEWLINE
     74             "      \"inner int\": 10" JSON_NEWLINE
     75             "   }, [  ], true ]" JSON_NEWLINE
     76             "}" JSON_NEWLINE,
     77             output_js);
     78 #undef JSON_NEWLINE
     79 
     80   // Test keys with periods
     81   DictionaryValue period_dict;
     82   period_dict.SetWithoutPathExpansion("a.b", Value::CreateIntegerValue(3));
     83   period_dict.SetWithoutPathExpansion("c", Value::CreateIntegerValue(2));
     84   DictionaryValue* period_dict2 = new DictionaryValue;
     85   period_dict2->SetWithoutPathExpansion("g.h.i.j",
     86                                         Value::CreateIntegerValue(1));
     87   period_dict.SetWithoutPathExpansion("d.e.f", period_dict2);
     88   JSONWriter::Write(&period_dict, false, &output_js);
     89   ASSERT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js);
     90 
     91   DictionaryValue period_dict3;
     92   period_dict3.Set("a.b", Value::CreateIntegerValue(2));
     93   period_dict3.SetWithoutPathExpansion("a.b", Value::CreateIntegerValue(1));
     94   JSONWriter::Write(&period_dict3, false, &output_js);
     95   ASSERT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js);
     96 }
     97 
     98 }  // namespace base
     99