public void TestFromJSONString()
{
var charbuf = new char[4];
CBORObject cbor;
// Test single-character strings
for (var i = 0; i < 0x110000; ++i) {
if (i >= 0xd800 && i < 0xe000) {
continue;
}
string str = this.CharString(i, true, charbuf);
if (i < 0x20 || i == 0x22 || i == 0x5c) {
TestFailingJSON(str);
} else {
cbor = TestSucceedingJSON(str);
string exp = this.CharString(i, false, charbuf);
if (!exp.Equals(cbor.AsString())) {
Assert.AreEqual(exp, cbor.AsString());
}
}
}
foreach (string str in jsonFails) {
TestFailingJSON(str);
}
foreach (string str in ValueJsonSucceeds) {
TestSucceedingJSON(str);
}
try {
CBORObject.FromJSONString("\ufeff\u0020 {}");
Assert.Fail("Should have failed");
} catch (CBORException) {
new Object();
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.FromJSONString("[]", null);
Assert.Fail("Should have failed");
} catch (ArgumentNullException) {
new Object();
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
TestFailingJSON("{\"a\":1,\"a\":2}", CBOREncodeOptions.NoDuplicateKeys);
string aba = "{\"a\":1,\"b\":3,\"a\":2}";
TestFailingJSON(aba, CBOREncodeOptions.NoDuplicateKeys);
cbor = TestSucceedingJSON(aba);
Assert.AreEqual(CBORObject.FromObject(2), cbor["a"]);
cbor = TestSucceedingJSON("{\"a\":1,\"a\":4}");
Assert.AreEqual(CBORObject.FromObject(4), cbor["a"]);
cbor = TestSucceedingJSON("\"\\t\"");
{
string stringTemp = cbor.AsString();
Assert.AreEqual(
"\t",
stringTemp);
}
Assert.AreEqual(CBORObject.True, TestSucceedingJSON("true"));
Assert.AreEqual(CBORObject.False, TestSucceedingJSON("false"));
Assert.AreEqual(CBORObject.Null, TestSucceedingJSON("null"));
Assert.AreEqual(5, TestSucceedingJSON(" 5 ").AsInt32());
{
string stringTemp = TestSucceedingJSON("\"\\/\\b\"").AsString();
Assert.AreEqual(
"/\b",
stringTemp);
}
{
string stringTemp = TestSucceedingJSON("\"\\/\\f\"").AsString();
Assert.AreEqual(
"/\f",
stringTemp);
}
string jsonTemp = TestCommon.Repeat(
"[",
2000) + TestCommon.Repeat(
"]",
2000);
TestFailingJSON(jsonTemp);
}