public void ChangeEntryPointLibraryNameJsonLab() { JsonObject obj = JsonObject.Parse(_dataUtf8); foreach (JsonObject target in obj["targets"]) { if (target.TryGetChild(out JsonObject firstChild)) { obj.Remove(firstChild); } } JsonObject libraries = obj["libraries"]; if (libraries.TryGetChild(out JsonObject child)) { obj.Remove(child); } _arrayOutput.Clear(); var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(_arrayOutput); jsonUtf8.Write(obj); jsonUtf8.Flush(); obj.Dispose(); }
private string CreateJson<T>(T sourceObject, string sharePointTypeName, IEnumerable<ListItemPropertyCreationInfo> properties) { string resultJson; using (var ms = new MemoryStream()) using (var writer = new Utf8JsonWriter(ms)) { writer.WriteStartObject(); writer.WriteStartObject("__metadata"); writer.WriteString("type", sharePointTypeName); writer.WriteEndObject(); foreach (var property in properties) { Type type = property.PropertyToSet.PropertyType; if (type.IsValueType) { writer.Write(sourceObject, property); } else { if (type == typeof(String)) { writer.Write(sourceObject, property); } else if (type == typeof(SharePointUrlField)) { writer.WriteSharePointUrlFieldObject(sourceObject, property); } else { ThrowNotImplementedException(type); } } } writer.WriteEndObject(); writer.Flush(); resultJson = Encoding.UTF8.GetString(ms.ToArray()); } return resultJson; }
public void ParseAndWriteSystemTextJsonLab() { JsonObject obj = JsonObject.Parse(_dataUtf8); _arrayOutput.Clear(); var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(_arrayOutput); jsonUtf8.Write(obj); jsonUtf8.Flush(); obj.Dispose(); }
public void ChangeEntryPointLibraryName() { string depsJson = TestJson.DepsJsonSignalR; byte[] dataUtf8 = Encoding.UTF8.GetBytes(depsJson); JsonObject obj = JsonObject.Parse(dataUtf8); var targetsString = new Utf8Span("targets"); var librariesString = new Utf8Span("libraries"); JsonObject targets = obj[targetsString]; foreach (JsonObject target in targets) { Assert.True(target.TryGetChild(out JsonObject firstChild)); obj.Remove(firstChild); } JsonObject libraries = obj[librariesString]; Assert.True(libraries.TryGetChild(out JsonObject child)); obj.Remove(child); string expected = ChangeEntryPointLibraryNameExpected(); var output = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8); var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(output); jsonUtf8.Write(obj); jsonUtf8.Flush(); ArraySegment <byte> formatted = output.Formatted; string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expected, actualStr); }
public void ParseJson(bool compactData, TestCaseType type, string jsonString) { // Remove all formatting/indendation if (compactData) { using (JsonTextReader jsonReader = new JsonTextReader(new StringReader(jsonString))) { jsonReader.FloatParseHandling = FloatParseHandling.Decimal; JToken jtoken = JToken.ReadFrom(jsonReader); var stringWriter = new StringWriter(); using (JsonTextWriter jsonWriter = new JsonTextWriter(stringWriter)) { jtoken.WriteTo(jsonWriter); jsonString = stringWriter.ToString(); } } } byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString); JsonObject obj = JsonObject.Parse(dataUtf8); var stream = new MemoryStream(dataUtf8); var streamReader = new StreamReader(stream, Encoding.UTF8, false, 1024, true); using (JsonTextReader jsonReader = new JsonTextReader(streamReader)) { JToken jtoken = JToken.ReadFrom(jsonReader); string expectedString = ""; string actualString = ""; if (type == TestCaseType.Json400KB) { expectedString = ReadJson400KB(jtoken); actualString = ReadJson400KB(obj); } else if (type == TestCaseType.HelloWorld) { expectedString = ReadHelloWorld(jtoken); actualString = ReadHelloWorld(obj); } Assert.Equal(expectedString, actualString); } string actual = obj.PrintJson(); // Change casing to match what JSON.NET does. actual = actual.Replace("true", "True").Replace("false", "False"); TextReader reader = new StringReader(jsonString); string expected = JsonTestHelper.NewtonsoftReturnStringHelper(reader); Assert.Equal(expected, actual); if (compactData) { var output = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8); var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(output); jsonUtf8.Write(obj); jsonUtf8.Flush(); ArraySegment <byte> formatted = output.Formatted; string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(jsonString, actualStr); } obj.Dispose(); }
public static void Write(this Utf8JsonWriter utf8JsonWriter, dynamic @dynamic) { if (dynamic == null || utf8JsonWriter == null) { return; } Type type = @dynamic?.GetType(); if (type == null) { return; } List <Tuple <string, object> > tuples = null; List <object> objects = null; switch (type.FullName) { case "IronPython.Runtime.PythonDictionary": if (Core.Query.TryInvokeDeclaredMethod(@dynamic, "items", out object pythonDictionary, new object[] { })) { if (Core.Query.TryInvokeDeclaredMethod(pythonDictionary, "GetObjectArray", out object pythonDictionaryArray, new object[] { })) { object[] objectArray = pythonDictionaryArray as object[]; if (objectArray != null) { tuples = new List <Tuple <string, object> >(); foreach (dynamic item in objectArray) { tuples.Add(new Tuple <string, object>(item[0], item[1])); } } } } break; case "IronPython.Runtime.List": if (Core.Query.TryInvokeDeclaredMethod(@dynamic, "GetObjectArray", out object list, new object[] { })) { object[] objectArray = list as object[]; if (objectArray != null) { if (objects == null) { objects = new List <object>(); } objects.AddRange(objectArray); } } break; case "IronPython.Runtime.PythonTuple": if (Core.Query.TryInvokeRuntimeMethod(@dynamic, "ToArray", out object tuple, new object[] { })) { object[] objectArray = tuple as object[]; if (objectArray != null) { if (objects == null) { objects = new List <object>(); } objects.AddRange(objectArray); } } break; default: if (@dynamic is IEnumerable) { if (objects == null) { objects = new List <object>(); } objects.AddRange(@dynamic); } else { string className = Query.ClassName(@dynamic); if (!string.IsNullOrWhiteSpace(className)) { object @object = null; try { @object = @dynamic.to_dict(); } catch { return; } if (@object == null) { return; } utf8JsonWriter.Write(@object); } } break; } if (tuples != null) { utf8JsonWriter.WriteStartObject(); foreach (Tuple <string, object> tuple in tuples) { string name = tuple.Item1; object value = tuple.Item2; if (value == null) { utf8JsonWriter.WriteNull(name); } else if (Core.Query.IsNumeric(value)) { utf8JsonWriter.WriteNumber(name, value as dynamic); } else if (value is string) { utf8JsonWriter.WriteString(name, (string)value); } else if (value is Guid) { utf8JsonWriter.WriteString(name, (Guid)value); } else if (value is DateTime) { utf8JsonWriter.WriteString(name, (DateTime)value); } else if (value is bool) { utf8JsonWriter.WriteBoolean(name, (bool)value); } else { utf8JsonWriter.WritePropertyName(name); utf8JsonWriter.Write(value); } } utf8JsonWriter.WriteEndObject(); } if (objects != null) { utf8JsonWriter.WriteStartArray(); foreach (object @object in objects) { if (@object == null) { utf8JsonWriter.WriteNullValue(); } else if (Core.Query.IsNumeric(@object)) { utf8JsonWriter.WriteNumberValue(@object as dynamic); } else if (@object is string) { utf8JsonWriter.WriteStringValue((string)@object); } else if (@object is Guid) { utf8JsonWriter.WriteStringValue((Guid)@object); } else if (@object is DateTime) { utf8JsonWriter.WriteStringValue((DateTime)@object); } else if (@object is bool) { utf8JsonWriter.WriteBooleanValue((bool)@object); } else { utf8JsonWriter.Write(@object); } } utf8JsonWriter.WriteEndArray(); } }
public override void Write( Utf8JsonWriter writer, IImmutableDictionary <TKey, TValue> value, JsonSerializerOptions options ) => writer.Write(value, options);