public void TestMultipleJsonArrayAsRootFail() { Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("[],")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("[\"abc\" : \"def\"],")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("[], []")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("[],\n[]")); }
public void TestOneJsonArray() { Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("[ 1 : 1 ]")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("[ 1 1 ]")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("[ 1 : \"1\" ]")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("[ \"1\" : \"1\" ]")); object parsed = JsonUtilities.Parse("[ 1 ]"); Assert.IsTrue(parsed is IList); IList parsedList = parsed as IList; Assert.AreEqual((long)1, parsedList[0]); parsed = JsonUtilities.Parse("[ \n ]"); Assert.IsTrue(parsed is IList); parsedList = parsed as IList; Assert.AreEqual(0, parsedList.Count); parsed = JsonUtilities.Parse("[ \"asdf\" ]"); Assert.IsTrue(parsed is IList); parsedList = parsed as IList; Assert.AreEqual("asdf", parsedList[0]); parsed = JsonUtilities.Parse("[ \"\u849c\" ]"); Assert.IsTrue(parsed is IList); parsedList = parsed as IList; Assert.AreEqual("\u849c", parsedList[0]); }
public void TestMultipleJsonObjectAsRootFail() { Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("{},")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("{\"abc\" : \"def\"},")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("{\"abc\" : \"def\" \"def\"}")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("{}, {}")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("{},\n{}")); }
private static object CoapGetJson(CoapRequest Request, object Payload) { StringBuilder sb = new StringBuilder(); ISensorDataExport SensorDataExport = new SensorDataJsonExport(sb); ExportSensorData(SensorDataExport, new ReadoutRequest(Request.ToHttpRequest())); return(JsonUtilities.Parse(sb.ToString())); }
public void TestJsonObjectWithElements() { // Just make sure they don't throw exception as we already check their content correctness // in other unit tests. JsonUtilities.Parse("{ \"mura\": \"masa\" }"); JsonUtilities.Parse("{ \"mura\": 1234 }"); JsonUtilities.Parse("{ \"mura\": { \"masa\": 1234 } }"); JsonUtilities.Parse("{ \"mura\": { \"masa\": [ 1234 ] } }"); JsonUtilities.Parse("{ \"mura\": { \"masa\": [ 1234 ] }, \"arr\": [] }"); }
public void TestJsonArrayWithElements() { // Just make sure they don't throw exception as we already check their content correctness // in other unit tests. JsonUtilities.Parse("[ \"mura\" ]"); JsonUtilities.Parse("[ \"\u1234\" ]"); JsonUtilities.Parse("[ \"\u1234ff\", \"\u1234\" ]"); JsonUtilities.Parse("[ [], [], [], [] ]"); JsonUtilities.Parse("[ [], [ {}, {} ], [ {} ], [] ]"); }
public void TestSpecialJsonNumbersAndModifiers() { Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("+123456789")); JsonUtilities.Parse("{ \"mura\": -123456789123456789 }"); JsonUtilities.Parse("{ \"mura\": 1.1234567891234567E308 }"); JsonUtilities.Parse("{ \"PI\": 3.141e-10 }"); JsonUtilities.Parse("{ \"PI\": 3.141E-10 }"); Assert.AreEqual(123456789123456789, (JsonUtilities.Parse("{ \"mura\": 123456789123456789 }") as IDictionary)["mura"]); }
public void TestOneJsonObject() { Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("{ 1 }")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("{ 1 : 1 }")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("{ 1 : \"abc\" }")); object parsed = JsonUtilities.Parse("{\"abc\" : \"def\"}"); Assert.IsTrue(parsed is IDictionary); IDictionary parsedDict = parsed as IDictionary; Assert.AreEqual("def", parsedDict["abc"]); parsed = JsonUtilities.Parse("{\"abc\" : {} }"); Assert.IsTrue(parsed is IDictionary); parsedDict = parsed as IDictionary; Assert.IsTrue(parsedDict["abc"] is IDictionary); parsed = JsonUtilities.Parse("{\"abc\" : \"6060\"}"); Assert.IsTrue(parsed is IDictionary); parsedDict = parsed as IDictionary; Assert.AreEqual("6060", parsedDict["abc"]); parsed = JsonUtilities.Parse("{\"\" : \"\"}"); Assert.IsTrue(parsed is IDictionary); parsedDict = parsed as IDictionary; Assert.AreEqual("", parsedDict[""]); parsed = JsonUtilities.Parse("{\" abc\" : \"def \"}"); Assert.IsTrue(parsed is IDictionary); parsedDict = parsed as IDictionary; Assert.AreEqual("def ", parsedDict[" abc"]); parsed = JsonUtilities.Parse("{\"1\" : 6060}"); Assert.IsTrue(parsed is IDictionary); parsedDict = parsed as IDictionary; Assert.AreEqual((long)6060, parsedDict["1"]); parsed = JsonUtilities.Parse("{\"1\" : null}"); Assert.IsTrue(parsed is IDictionary); parsedDict = parsed as IDictionary; Assert.IsNull(parsedDict["1"]); parsed = JsonUtilities.Parse("{\"1\" : true}"); Assert.IsTrue(parsed is IDictionary); parsedDict = parsed as IDictionary; Assert.IsTrue((bool)parsedDict["1"]); parsed = JsonUtilities.Parse("{\"1\" : false}"); Assert.IsTrue(parsed is IDictionary); parsedDict = parsed as IDictionary; Assert.IsFalse((bool)parsedDict["1"]); }
public void TestInvalidJsonStringAsRootFail() { Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("\n")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("a")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("abc")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("\u1234")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("\t")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("\t\n\r")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse(" ")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("1234")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("1,3")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("{1")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("3}")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("}")); }
internal Task LoadAsync() => File.ReadAllTextAsync().ContinueWith(task => { lock (Mutex) { try { Storage = JsonUtilities.Parse(task.Result) as Dictionary <string, object>; } catch { Storage = new Dictionary <string, object> { }; } } });
public void TestGeoPointCultureInvariantParsing() { CultureInfo initialCulture = Thread.CurrentThread.CurrentCulture; foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.AllCultures)) { Thread.CurrentThread.CurrentCulture = culture; ParseGeoPoint point = new ParseGeoPoint(1.234, 1.234); IDictionary <string, object> deserialized = Client.Decoder.Decode(JsonUtilities.Parse(JsonUtilities.Encode(new Dictionary <string, object> { [nameof(point)] = NoObjectsEncoder.Instance.Encode(point, Client) })), Client) as IDictionary <string, object>; ParseGeoPoint pointAgain = (ParseGeoPoint)deserialized[nameof(point)]; Assert.AreEqual(1.234, pointAgain.Latitude); Assert.AreEqual(1.234, pointAgain.Longitude); } Thread.CurrentThread.CurrentCulture = initialCulture; }
public void TestJsonArrayInsideJsonObject() { Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("{ [] }")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("{ [], [] }")); Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("{ \"abc\": [], [] }")); object parsed = JsonUtilities.Parse("{ \"abc\": [] }"); Assert.IsTrue(parsed is IDictionary); IDictionary parsedDict = parsed as IDictionary; Assert.IsTrue(parsedDict["abc"] is IList); parsed = JsonUtilities.Parse("{ \"6060\" :\n[ 6060 ]\t}"); Assert.IsTrue(parsed is IDictionary); parsedDict = parsed as IDictionary; Assert.IsTrue(parsedDict["6060"] is IList); IList parsedList = parsedDict["6060"] as IList; Assert.AreEqual((long)6060, parsedList[0]); }
public void TestJsonObjectInsideJsonArray() { Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse("[ {} : {} ]")); // whitespace test object parsed = JsonUtilities.Parse("[\t\n{}\r\t]"); Assert.IsTrue(parsed is IList); IList parsedList = parsed as IList; Assert.IsTrue(parsedList[0] is IDictionary); parsed = JsonUtilities.Parse("[ {}, { \"final\" : \"fantasy\"} ]"); Assert.IsTrue(parsed is IList); parsedList = parsed as IList; Assert.IsTrue(parsedList[0] is IDictionary); Assert.IsTrue(parsedList[1] is IDictionary); IDictionary parsedDictionary = parsedList[1] as IDictionary; Assert.AreEqual("fantasy", parsedDictionary["final"]); }
// TODO: (richardross) investigate this. // Obj-C type -> .NET type is impossible to do flawlessly (especially // on NSNumber). We can't transform NSDictionary into string because of this reason. internal ParsePushNotificationEvent(string stringPayload) { TextContent = stringPayload; Content = JsonUtilities.Parse(stringPayload) as IDictionary <string, object>; }
public void TestEmptyJsonStringFail() => Assert.ThrowsException <ArgumentException>(() => JsonUtilities.Parse(""));
private void ParseMessage(string message) { try { IDictionary <string, object> jsonObject = (IDictionary <string, object>)JsonUtilities.Parse(message); string rawOperation = (string)jsonObject["op"]; switch (rawOperation) { case "connected": _hasReceivedConnected = true; DispatchConnected(); foreach (Subscription subscription in _subscriptions.Values) { SendSubscription(subscription); } break; case "redirect": // TODO: Handle redirect. //string url = (string) jsonObject["url"]; break; case "subscribed": HandleSubscribedEvent(jsonObject); break; case "unsubscribed": HandleUnsubscribedEvent(jsonObject); break; case "enter": HandleObjectEvent(Subscription.Event.Enter, jsonObject); break; case "leave": HandleObjectEvent(Subscription.Event.Leave, jsonObject); break; case "update": HandleObjectEvent(Subscription.Event.Update, jsonObject); break; case "create": HandleObjectEvent(Subscription.Event.Create, jsonObject); break; case "delete": HandleObjectEvent(Subscription.Event.Delete, jsonObject); break; case "error": HandleErrorEvent(jsonObject); break; default: throw new InvalidResponseException(message); } } catch (Exception e) when(!(e is LiveQueryException)) { throw new InvalidResponseException(message, e); } }
public void TestEmptyJsonArray() => Assert.IsTrue(JsonUtilities.Parse("[]") is IList);
public void TestEmptyJsonObject() => Assert.IsTrue(JsonUtilities.Parse("{}") is IDictionary);