예제 #1
0
파일: test.cs 프로젝트: mono/gert
		static int Main (string [] args)
		{
			JsonObject obj = new JsonObject ();
			string json;

#if !MONO
			MemoryStream ms = new MemoryStream ();
			DataContractJsonSerializer dcSerializer = new DataContractJsonSerializer (typeof (JsonObject));
			dcSerializer.WriteObject (ms, obj);
			ms.Position = 0;

			StreamReader sr = new StreamReader (ms);
			json = sr.ReadToEnd ();
			Assert.AreEqual ("{\"int_key\":null}", json, "#1");

			ms = new MemoryStream ();
			StreamWriter sw = new StreamWriter (ms);
			sw.Write (json);
			sw.Flush ();
			ms.Position = 0;

			obj = (JsonObject) dcSerializer.ReadObject (ms);
			Assert.IsNull (obj.int_key, "#2");
#endif

			json = "{\"int_key\":null}";

			JavaScriptSerializer jsSerializer = new JavaScriptSerializer ();
			obj = jsSerializer.Deserialize<JsonObject> (json);
			Assert.IsNull (obj.int_key, "#3");

			json = "{\"int_key\" : \"\"}";

#if !MONO
			ms = new MemoryStream ();
			sw = new StreamWriter (ms);
			sw.Write (json);
			sw.Flush ();
			ms.Position = 0;

			try {
				obj = (JsonObject) dcSerializer.ReadObject (ms);
				Assert.Fail ("#4");
			} catch (SerializationException) {
			}
#endif

			obj = jsSerializer.Deserialize<JsonObject> (json);
			Assert.IsNull (obj.int_key, "#5");

			return 0;
		}
예제 #2
0
        static void Main(string[] args)
        {
            var inputString = @"{
                ""firstName"":""Иван"",
                ""lastName"":""Иванов"",
                ""test"":""test1\ntest2\ntest3\\"",
                ""address"":{
                    ""streetAddress"":""Московское ш., 101, кв.101"",
                    ""city"":""Ленинград"",
                    ""postalCode"":101101
                },
                ""phoneNumbers"":[
                    ""812 123-1234"",
                    ""916 123-4567""
                ]
            }";
            var json = (JsonObject)JsonParser.Parse(inputString);
            Console.WriteLine("First name: {0}", json["firstName"]);
            Console.WriteLine("Last name: {0}", json["lastName"]);
            var address = (JsonObject)json["address"];
            foreach (var el in address.Keys) Console.WriteLine("{0} = {1}", el, address[el]);
            var phoneNumbers = (JsonArray)json["phoneNumbers"];
            foreach (var el in phoneNumbers) Console.WriteLine("Phone: {0}", el);
            var encodedJson = json.Encode();
            var decodedJson = JsonParser.Parse(encodedJson);
            Debug.Assert(encodedJson == decodedJson.Encode());
            Console.WriteLine("Encoded JSON: {0}", encodedJson);

            var testJson = new JsonObject();
            testJson["hello"] = new JsonValue("world");
            testJson["escaped_string"] = new JsonValue(@"Blabla""[123]\{abc}");
            var author = new JsonObject();
            author["name"] = new JsonValue("Cluster");
            author["age"] = new JsonValue(26);
            var contacts = new JsonArray();
            contacts.Add(new JsonValue("http://clusterrr.com"));
            contacts.Add(new JsonValue("*****@*****.**"));
            author["contacts"] = contacts;
            testJson["author"] = author;
            encodedJson = testJson.Encode();
            decodedJson = JsonParser.Parse(encodedJson);
            Debug.Assert(encodedJson == decodedJson.Encode());
            Console.WriteLine("Encoded JSON: {0}", encodedJson);
            Console.ReadLine();
        }