public static void LogListPredicateDeserializationFailure(DataModelConditionListPredicate dataModelConditionPredicate, JsonException exception) { _logger.Warning( exception, "Failed to deserialize display condition list predicate {list} => {left} {operator} {right}", dataModelConditionPredicate.Entity.LeftPath?.Path, dataModelConditionPredicate.Entity.OperatorType, dataModelConditionPredicate.Entity.RightPath?.Path ); }
private void sendJsonParseError(JsonException e, HttpResponse response) { sendError(response, new ResponseItem(SC_JSON_PARSE_ERROR, "Invalid JSON - " + e.Message)); }
public async Task PathForSpecialCharacterNestedFails() { JsonException e = await Assert.ThrowsAsync <JsonException>(() => Serializer.DeserializeWrapper <RootClass>(@"{""Child"":{""Children"":[{}, {""MyDictionary"":{""K.e.y"": {""MyInt"":bad")); Assert.Equal("$.Child.Children[1].MyDictionary['K.e.y'].MyInt", e.Path); }
public static void DuplicateNameEnumTest() { JsonException e = Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <DuplicateNameEnum>("\"foo_bar\"")); }
public async Task PathForChildPropertyFails() { JsonException e = await Assert.ThrowsAsync <JsonException>(() => Serializer.DeserializeWrapper <RootClass>(@"{""Child"":{""MyInt"":bad]}")); Assert.Equal("$.Child.MyInt", e.Path); }
public async Task PathForChildDictionaryFails() { JsonException e = await Assert.ThrowsAsync <JsonException>(() => Serializer.DeserializeWrapper <RootClass>(@"{""Child"":{""MyDictionary"":{""Key"": bad]")); Assert.Equal("$.Child.MyDictionary.Key", e.Path); }
public void PathForChildDictionaryFails() { JsonException e = Assert.Throws <JsonException>(() => Serializer.Deserialize <RootClass>(@"{""Child"":{""MyDictionary"":{""Key"": bad]")); Assert.Equal("$.Child.MyDictionary.Key", e.Path); }
public override void TestFixtureSetUp() { #if !MONO Helper.EatException(() => _dumpResponse = CreateDumpResponse()); Helper.EatException(() => _dumpResponse2 = CreateDumpResponse2()); Helper.EatException(() => _dumpRequest = CreateDumpRequest()); Helper.EatException(() => _dumpRequest2 = CreateDumpRequest2()); #endif base.TestFixtureSetUp(); _createdMiniNode = false; if (SetUpFixture._connection != null && SetUpFixture._node != null) { _tag = "_" + (++SetUpFixture._counter); _node = SetUpFixture._node; _connection = SetUpFixture._connection; } else { _createdMiniNode = true; _tag = "_1"; _node = CreateMiniNode(); _node.Start(); _connection = TestConnection.Create(_node.TcpEndPoint); _connection.ConnectAsync().Wait(); } _lastResponse = null; _lastResponseBody = null; _lastResponseBytes = null; _lastJsonException = null; try { Given(); When(); } catch { if (_createdMiniNode) { if (_connection != null) { try { _connection.Close(); } catch { } } if (_node != null) { try { _node.Shutdown(); } catch { } } } throw; } }
public void PathForChildPropertyFails() { JsonException e = Assert.Throws <JsonException>(() => Serializer.Deserialize <RootClass>(@"{""Child"":{""MyInt"":bad]}")); Assert.Equal("$.Child.MyInt", e.Path); }
public void PathForChildListFails() { JsonException e = Assert.Throws <JsonException>(() => Serializer.Deserialize <RootClass>(@"{""Child"":{""MyIntArray"":[1, bad]}")); Assert.Contains("$.Child.MyIntArray", e.Path); }
public NewsJSONException(string message, JsonException inner) : base(message, inner) { }
public async void ClassWithRequiredKeywordAndLargeParametrizedCtorFailsDeserialization(bool ignoreNullValues) { JsonSerializerOptions options = new() { IgnoreNullValues = ignoreNullValues }; AssertJsonTypeInfoHasRequiredProperties(GetTypeInfo <PersonWithRequiredMembersAndLargeParametrizedCtor>(options), nameof(PersonWithRequiredMembersAndLargeParametrizedCtor.AProp), nameof(PersonWithRequiredMembersAndLargeParametrizedCtor.BProp), nameof(PersonWithRequiredMembersAndLargeParametrizedCtor.CProp), nameof(PersonWithRequiredMembersAndLargeParametrizedCtor.DProp), nameof(PersonWithRequiredMembersAndLargeParametrizedCtor.EProp), nameof(PersonWithRequiredMembersAndLargeParametrizedCtor.FProp), nameof(PersonWithRequiredMembersAndLargeParametrizedCtor.GProp), nameof(PersonWithRequiredMembersAndLargeParametrizedCtor.HProp), nameof(PersonWithRequiredMembersAndLargeParametrizedCtor.IProp)); var obj = new PersonWithRequiredMembersAndLargeParametrizedCtor("bada", "badb", "badc", "badd", "bade", "badf", "badg") { // note: these must be set during initialize or otherwise we get compiler errors AProp = "a", BProp = "b", CProp = "c", DProp = "d", EProp = "e", FProp = "f", GProp = "g", HProp = "h", IProp = "i", }; string json = await Serializer.SerializeWrapper(obj, options); Assert.Equal("" "{" AProp ":" a "," BProp ":" b "," CProp ":" c "," DProp ":" d "," EProp ":" e "," FProp ":" f "," GProp ":" g "," HProp ":" h "," IProp ":" i "}" "", json); var deserialized = await Serializer.DeserializeWrapper <PersonWithRequiredMembersAndLargeParametrizedCtor>(json, options); Assert.Equal(obj.AProp, deserialized.AProp); Assert.Equal(obj.BProp, deserialized.BProp); Assert.Equal(obj.CProp, deserialized.CProp); Assert.Equal(obj.DProp, deserialized.DProp); Assert.Equal(obj.EProp, deserialized.EProp); Assert.Equal(obj.FProp, deserialized.FProp); Assert.Equal(obj.GProp, deserialized.GProp); Assert.Equal(obj.HProp, deserialized.HProp); Assert.Equal(obj.IProp, deserialized.IProp); json = "" "{" AProp ":" a "," BProp ":" b "," CProp ":" c "," DProp ":" d "," EProp ":null," FProp ":" f "," GProp ":" g "," HProp ":null," IProp ":" i "}" ""; deserialized = await Serializer.DeserializeWrapper <PersonWithRequiredMembersAndLargeParametrizedCtor>(json, options); Assert.Equal(obj.AProp, deserialized.AProp); Assert.Equal(obj.BProp, deserialized.BProp); Assert.Equal(obj.CProp, deserialized.CProp); Assert.Equal(obj.DProp, deserialized.DProp); Assert.Null(deserialized.EProp); Assert.Equal(obj.FProp, deserialized.FProp); Assert.Equal(obj.GProp, deserialized.GProp); Assert.Null(deserialized.HProp); Assert.Equal(obj.IProp, deserialized.IProp); json = "" "{" AProp ":" a "," IProp ":" i "}" ""; JsonException exception = await Assert.ThrowsAsync <JsonException>(async() => await Serializer.DeserializeWrapper <PersonWithRequiredMembersAndLargeParametrizedCtor>(json, options)); Assert.DoesNotContain("AProp", exception.Message); Assert.Contains("BProp", exception.Message); Assert.Contains("CProp", exception.Message); Assert.Contains("DProp", exception.Message); Assert.Contains("EProp", exception.Message); Assert.Contains("FProp", exception.Message); Assert.Contains("GProp", exception.Message); Assert.Contains("HProp", exception.Message); Assert.DoesNotContain("IProp", exception.Message); json = "" "{" AProp ":null," IProp ":null}" ""; exception = await Assert.ThrowsAsync <JsonException>(async() => await Serializer.DeserializeWrapper <PersonWithRequiredMembersAndLargeParametrizedCtor>(json, options)); Assert.DoesNotContain("AProp", exception.Message); Assert.Contains("BProp", exception.Message); Assert.Contains("CProp", exception.Message); Assert.Contains("DProp", exception.Message); Assert.Contains("EProp", exception.Message); Assert.Contains("FProp", exception.Message); Assert.Contains("GProp", exception.Message); Assert.Contains("HProp", exception.Message); Assert.DoesNotContain("IProp", exception.Message); json = "" "{" BProp ":" b "," CProp ":" c "," DProp ":" d "," EProp ":" e "," FProp ":" f "," HProp ":" h "}" ""; exception = await Assert.ThrowsAsync <JsonException>(async() => await Serializer.DeserializeWrapper <PersonWithRequiredMembersAndLargeParametrizedCtor>(json, options)); Assert.Contains("AProp", exception.Message); Assert.DoesNotContain("BProp", exception.Message); Assert.DoesNotContain("CProp", exception.Message); Assert.DoesNotContain("DProp", exception.Message); Assert.DoesNotContain("EProp", exception.Message); Assert.DoesNotContain("FProp", exception.Message); Assert.Contains("GProp", exception.Message); Assert.DoesNotContain("HProp", exception.Message); Assert.Contains("IProp", exception.Message); }
public async void ClassWithRequiredKeywordAndSmallParametrizedCtorFailsDeserialization(bool ignoreNullValues) { JsonSerializerOptions options = new() { IgnoreNullValues = ignoreNullValues }; AssertJsonTypeInfoHasRequiredProperties(GetTypeInfo <PersonWithRequiredMembersAndSmallParametrizedCtor>(options), nameof(PersonWithRequiredMembersAndSmallParametrizedCtor.FirstName), nameof(PersonWithRequiredMembersAndSmallParametrizedCtor.LastName), nameof(PersonWithRequiredMembersAndSmallParametrizedCtor.Info1), nameof(PersonWithRequiredMembersAndSmallParametrizedCtor.Info2)); var obj = new PersonWithRequiredMembersAndSmallParametrizedCtor("badfoo", "badbar") { // note: these must be set during initialize or otherwise we get compiler errors FirstName = "foo", LastName = "bar", Info1 = "info1", Info2 = "info2", }; string json = await Serializer.SerializeWrapper(obj, options); Assert.Equal("" "{" FirstName ":" foo "," MiddleName ":" "," LastName ":" bar "," Info1 ":" info1 "," Info2 ":" info2 "}" "", json); var deserialized = await Serializer.DeserializeWrapper <PersonWithRequiredMembersAndSmallParametrizedCtor>(json, options); Assert.Equal(obj.FirstName, deserialized.FirstName); Assert.Equal(obj.MiddleName, deserialized.MiddleName); Assert.Equal(obj.LastName, deserialized.LastName); Assert.Equal(obj.Info1, deserialized.Info1); Assert.Equal(obj.Info2, deserialized.Info2); json = "" "{" FirstName ":" foo "," MiddleName ":" "," LastName ":null," Info1 ":null," Info2 ":" info2 "}" ""; deserialized = await Serializer.DeserializeWrapper <PersonWithRequiredMembersAndSmallParametrizedCtor>(json, options); Assert.Equal(obj.FirstName, deserialized.FirstName); Assert.Equal(obj.MiddleName, deserialized.MiddleName); Assert.Null(deserialized.LastName); Assert.Null(deserialized.Info1); Assert.Equal(obj.Info2, deserialized.Info2); json = "" "{" LastName ":" bar "," Info1 ":" info1 "}" ""; JsonException exception = await Assert.ThrowsAsync <JsonException>(async() => await Serializer.DeserializeWrapper <PersonWithRequiredMembersAndSmallParametrizedCtor>(json, options)); Assert.Contains("FirstName", exception.Message); Assert.DoesNotContain("LastName", exception.Message); Assert.DoesNotContain("MiddleName", exception.Message); Assert.DoesNotContain("Info1", exception.Message); Assert.Contains("Info2", exception.Message); json = "" "{" LastName ":null," Info1 ":null}" ""; exception = await Assert.ThrowsAsync <JsonException>(async() => await Serializer.DeserializeWrapper <PersonWithRequiredMembersAndSmallParametrizedCtor>(json, options)); Assert.Contains("FirstName", exception.Message); Assert.DoesNotContain("LastName", exception.Message); Assert.DoesNotContain("MiddleName", exception.Message); Assert.DoesNotContain("Info1", exception.Message); Assert.Contains("Info2", exception.Message); json = "{}"; exception = await Assert.ThrowsAsync <JsonException>(async() => await Serializer.DeserializeWrapper <PersonWithRequiredMembersAndSmallParametrizedCtor>(json, options)); Assert.Contains("FirstName", exception.Message); Assert.Contains("LastName", exception.Message); Assert.DoesNotContain("MiddleName", exception.Message); Assert.Contains("Info1", exception.Message); Assert.Contains("Info2", exception.Message); }
public ParseResponseException(JsonException jsonException, string sourceData) { JsonException = jsonException; SourceData = sourceData; }
public void EscapingFails() { JsonException e = Assert.Throws <JsonException>(() => Serializer.Deserialize <Parameterized_ClassWithUnicodeProperty>("{\"A\u0467\":bad}")); Assert.Equal("$.A\u0467", e.Path); }
public DnsPodResponseException(string apiPath, JsonException jsonException, string responseContent, HttpStatusCode statusCode) : base("Could not read response object from API response returned from DNSPod API " + apiPath, jsonException) { this.ResponseContent = responseContent; this.ResponseStatusCode = statusCode; }
public void ExtensionPropertyRoundTripFails() { JsonException e = Assert.Throws <JsonException>(() => Serializer.Deserialize <Parameterized_ClassWithExtensionProperty>(@"{""MyNestedClass"":{""UnknownProperty"":bad}}")); Assert.Equal("$.MyNestedClass.UnknownProperty", e.Path); }
public static Key DecodeToken(string text, Token token) { if (token.Type != TokenType.EncodedString) { return(new Key(text.Substring(token.Start, token.Length), token.Hash)); } int cur = token.Start; StringBuilder value = new StringBuilder(token.Length); StringHasher hasher = StringHasher.Create(); for (int end = cur + token.Length; cur != -1 && cur < end;) { char ch = text[cur]; if (ch == '\\') { ch = (cur + 1 < end) ? text[cur + 1] : '\0'; switch (ch) { case '\"': case '\\': case '/': hasher.AddChar(ch); value.Append(ch); cur += 2; break; case 'b': hasher.AddChar('\b'); value.Append('\b'); cur += 2; break; case 'f': hasher.AddChar('\f'); value.Append('\f'); cur += 2; break; case 'n': hasher.AddChar('\n'); value.Append('\n'); cur += 2; break; case 'r': hasher.AddChar('\r'); value.Append('\r'); cur += 2; break; case 't': hasher.AddChar('\t'); value.Append('\t'); cur += 2; break; case 'u': if (cur + 5 < end) { string buffer = text.Substring(cur + 2, 4); if (uint.TryParse(buffer, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out uint decoded)) { ch = (char)decoded; hasher.AddChar(ch); value.Append(ch); cur += 6; } else { cur = -1; } } else { cur = -1; } break; default: cur = -1; break; } } else { hasher.AddChar(ch); value.Append(ch); cur++; } } if (cur == -1) { throw JsonException.New(Resources.Parser_InvalidStringToken); } return(new Key(value.ToString(), hasher.HashValue)); }
public string ParseString(string JsonString, bool TextFields = false, bool FromAH = false) { string PE; _fromAH = FromAH; string FunctionName = MReference.VedAPI.FunctionName; try { JObject PJE = JObject.Parse(JsonString); PE = fixFormatting(PJE.ToString(), false); if (TextFields) { FieldList.Clear(); JsonParser(PJE); JsonObject = PJE; } if (MReference.VedAPI.Authenticate) { MReference.VedAPI.ApiKey = PJE.SelectToken("APIKey").ToString(); return(string.Empty); } if (MReference.VedAPI.APITest) { Paths.Clear(); ResponseJson = PJE; IEnumerable <string> DescPaths = PJE.Descendants().Where(y => y.Path.Contains(".")).Select(x => x.Path.Substring(x.Path.IndexOf('.'))).Distinct(); foreach (JToken PI in PJE.DescendantsAndSelf()) { JTokenType JType = PI.Type; if (JType == JTokenType.Array || JType == JTokenType.Object) { if (PI.Path != string.Empty) { Paths.Add(PI.Path); if (!PI.Path.Contains("[")) { foreach (string P in DescPaths) { Paths.Add(PI.Path + P); } } } } else if (JType != JTokenType.Property) { if (PI.Path != string.Empty) { string OPTList = PI.Path + " - " + ResponseJson.SelectToken(PI.Path).ToString(); Paths.Add(OPTList); } } } if (MReference.VedAPI.FNames[FunctionName] != null) { MReference.VedAPI.FNames[FunctionName].Clear(); foreach (string LI in Paths) { if (!MReference.VedAPI.FNames[FunctionName].Contains(LI)) { MReference.VedAPI.FNames[FunctionName].Add(LI); } } } } JsonError = null; } catch (JsonException JE) { JsonError = JE; PE = JE.Message; } return(PE); }
public async Task PathForChildListFails() { JsonException e = await Assert.ThrowsAsync <JsonException>(() => Serializer.DeserializeWrapper <RootClass>(@"{""Child"":{""MyIntArray"":[1, bad]}")); Assert.Contains("$.Child.MyIntArray", e.Path); }
public void ThrowOnInvalidFormat(string json, Type typeToConvert) { JsonException ex = Assert.Throws <JsonException>(() => JsonSerializer.Deserialize(json, typeToConvert)); Assert.Contains(typeToConvert.ToString(), ex.Message); }
public async Task PathForSpecialCharacterFails() { JsonException e = await Assert.ThrowsAsync <JsonException>(() => Serializer.DeserializeWrapper <RootClass>(@"{""Child"":{""MyDictionary"":{""Key1"":{""Children"":[{""MyDictionary"":{""K.e.y"":""")); Assert.Equal("$.Child.MyDictionary.Key1.Children[0].MyDictionary['K.e.y']", e.Path); }
private static void Convert(string f) { string magic; string xmlMagic; using (FileStream fs = File.Open(f, FileMode.Open, FileAccess.Read, FileShare.Read)) { PkgBinaryReader reader = new PkgBinaryReader(fs); magic = reader.ReadString(4); // Skip first byte since BXMLBig starts with \0 causing empty string reader.Seek(1, SeekOrigin.Begin); xmlMagic = reader.ReadString(3); } if (xmlMagic == "\"Rr" || xmlMagic == "BXM") { XmlFile file = new XmlFile(File.Open(f, FileMode.Open, FileAccess.Read, FileShare.Read)); file.Write(File.Open(f + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read), XMLType.Text); Console.WriteLine("Success! XML converted."); } else if (magic == "LNGT") { LngFile file = new LngFile(File.Open(f, FileMode.Open, FileAccess.Read, FileShare.Read)); file.WriteXml(File.Open(f + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read)); Console.WriteLine("Success! Lng converted."); } else if (magic == "!pkg") { PkgFile file = PkgFile.ReadPkg(File.Open(f, FileMode.Open, FileAccess.Read, FileShare.Read)); file.WriteJson(File.Open(f + ".json", FileMode.Create, FileAccess.Write, FileShare.Read)); Console.WriteLine("Success! Pkg converted."); } else { bool isJSON = false; JsonException jsonEx = null; try { PkgFile pkgFile = PkgFile.ReadJson(File.Open(f, FileMode.Open, FileAccess.Read, FileShare.Read)); pkgFile.WritePkg(File.Open(f + ".pkg", FileMode.Create, FileAccess.Write, FileShare.Read)); Console.WriteLine("Success! JSON converted."); isJSON = true; } catch (JsonException e) { jsonEx = e; } if (!isJSON) { XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(f); } catch (XmlException e) { throw new AggregateException("Could not determine the file type! Showing json, and xml errors: ", jsonEx, e); } if (xmlDoc.DocumentElement.Name == "language") { DataSet dataSet = new DataSet("language"); dataSet.ReadXml(File.Open(f, FileMode.Open, FileAccess.Read, FileShare.Read), XmlReadMode.ReadSchema); LngFile file = new LngFile(dataSet); file.Write(File.Open(f + ".lng", FileMode.Create, FileAccess.Write, FileShare.Read)); Console.WriteLine("Success! XML converted."); } else { XmlFile file = new XmlFile(File.Open(f, FileMode.Open, FileAccess.Read, FileShare.Read)); file.Write(File.Open(f + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read)); Console.WriteLine("Success! XML converted."); } } } }
public async Task EscapingFails() { JsonException e = await Assert.ThrowsAsync <JsonException>(() => Serializer.DeserializeWrapper <Parameterized_ClassWithUnicodeProperty>("{\"A\u0467\":bad}")); Assert.Equal("$.A\u0467", e.Path); }
public static void JsonException(JsonException ex) { WriteGenericLog(ex); }