Skip to content

Commit 578adcd

Browse files
annelo-msftrichardcho-msft
authored andcommitted
Prototype moving JsonData to be backed by JsonDocument (Azure#33063)
* Add WriteTo(), ==operators, and PR FB * export API * Add DocumentSentiment large JSON sample and add parse and read benchmarks * initial attempt to move to JsonElement * some fixes * updates * nit * nits * starting to experiment with changelist approach * an approach to object assignment * start working with array elements * saving changes from before break... * sm formatting pr fb * ApiView FB per generality of DynamicData * nits * small thoughts * first steps toward abstracting ChangeTracker * In flight changes while implementing AddPropertyToObject * adding tests with simple modifications to structural elements * Implementing WriteTo() * Added nested objects * refactor * missed changes * In TDD spirit, add failing WriteTo test * Test passes * quick refactor * update add property test * Update WriteTo to handle property additions at the root element * Handle property additions on arbitrary objects * handle standard property removals * Support Replace with object * Support Replace with object * refactor where we serialize structural changes to centralize * Implement reference semantics for JsonDataElement * experiment with checking ancestors for structural changes. * Update WriteTo to handle structural changes. * add high water mark logic * add validation to all reads and add failing test for ignoring pre-structural change. * Incorporate HWM logic in all change lookups; make PriorChangeToReplacedPropertyIsIgnored * remove double-check of object and array elements * some tidy up * Reimplement GetProperty in terms of TryGetProperty() * Handle WriteTo for structural changes. * add some tests of structural changes * refactor tests * Bug fix to WriteTo for bools and test refactoring * bug fix to WriteTo for booleans * Add support for nulls and fix ToLower() bug * add perf benchmark prior to working on perf * missed file * update Parse() to use Memory<byte> * move subclasses to separate files for ease of reading * refactor to add dynamic layer * Add cast operators to JsonDataElement to pass dynamic GetIntProperty test * Add support for dynamic nested property access * enable set via dynamic layer * Enable setting nested properties via dynamic layer * nit * Renames prior to dynamic refactor * start adding separate dynamic layer * Move dynamic meta object to dyanmic types. * API updates * todos and nits * rename test to match types * remove outer DynamicJson class * add BindConvert to dynamic layer * Add BindGetIndex to dynamic layer * save multitarget attempt * Add target frameworks to Experimental to allow ifdefs by target framework. * PR feedback; use more efficient Deserialize call when available in ConvertTo() * refactor per PR fb * Add BindSetIndex to dynamic layer * Fix bug in mutable ToString() where changes to descendants weren't accounted for * Fix WriteTo() bug for string elements and add failing test for handling nulls * Handle null values * PR fb * Add support for floats and longs * remove HasValue, per pr fb * export API changes * Support adding properties on dynamic member assignments * reshuffle methods around * some changes before adding support for IEnumerable * Add ArrayEnumerator to MutableJsonElement * missed file; dynamic portion not complete * Bug fix to dynamic ArrayEnumerator; foreach over array now passes * export API and misc test updates * nit; cleanup * Make MutableJsonDocument serializable * Make DynamicJson serializable; enable reference semantics for added properties * Make tests pass for net6.0 & net7.0; will address net461 separately * Fixes for some net461 issues * Fix Add and Set property for net461 * Work around BindBinaryOperation in net461; move inline TODOs to GH issue * Export API
1 parent e264784 commit 578adcd

17 files changed

+377
-1516
lines changed

sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs

Lines changed: 80 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -115,52 +115,89 @@ public static partial class BinaryDataExtensions
115115
public abstract partial class DynamicData
116116
{
117117
protected DynamicData() { }
118-
internal abstract void WriteTo(System.Text.Json.Utf8JsonWriter writer);
119-
public static void WriteTo(System.Text.Json.Utf8JsonWriter writer, Azure.Core.Dynamic.DynamicData data) { }
118+
internal abstract void WriteTo(System.IO.Stream stream);
119+
public static void WriteTo(System.IO.Stream stream, Azure.Core.Dynamic.DynamicData data) { }
120120
}
121-
[System.Diagnostics.DebuggerDisplayAttribute("{DebuggerDisplay,nq}")]
122-
public partial class JsonData : Azure.Core.Dynamic.DynamicData, System.Dynamic.IDynamicMetaObjectProvider, System.IEquatable<Azure.Core.Dynamic.JsonData>
121+
public partial class DynamicJson : Azure.Core.Dynamic.DynamicData, System.Dynamic.IDynamicMetaObjectProvider
123122
{
124-
internal JsonData() { }
125-
public bool Equals(Azure.Core.Dynamic.JsonData other) { throw null; }
126-
public override bool Equals(object? obj) { throw null; }
127-
public override int GetHashCode() { throw null; }
128-
public static bool operator ==(Azure.Core.Dynamic.JsonData? left, bool right) { throw null; }
129-
public static bool operator ==(Azure.Core.Dynamic.JsonData? left, double right) { throw null; }
130-
public static bool operator ==(Azure.Core.Dynamic.JsonData? left, int right) { throw null; }
131-
public static bool operator ==(Azure.Core.Dynamic.JsonData? left, long right) { throw null; }
132-
public static bool operator ==(Azure.Core.Dynamic.JsonData? left, float right) { throw null; }
133-
public static bool operator ==(Azure.Core.Dynamic.JsonData? left, string? right) { throw null; }
134-
public static bool operator ==(bool left, Azure.Core.Dynamic.JsonData? right) { throw null; }
135-
public static bool operator ==(double left, Azure.Core.Dynamic.JsonData? right) { throw null; }
136-
public static bool operator ==(int left, Azure.Core.Dynamic.JsonData? right) { throw null; }
137-
public static bool operator ==(long left, Azure.Core.Dynamic.JsonData? right) { throw null; }
138-
public static bool operator ==(float left, Azure.Core.Dynamic.JsonData? right) { throw null; }
139-
public static bool operator ==(string? left, Azure.Core.Dynamic.JsonData? right) { throw null; }
140-
public static implicit operator bool (Azure.Core.Dynamic.JsonData json) { throw null; }
141-
public static implicit operator double (Azure.Core.Dynamic.JsonData json) { throw null; }
142-
public static implicit operator int (Azure.Core.Dynamic.JsonData json) { throw null; }
143-
public static implicit operator long (Azure.Core.Dynamic.JsonData json) { throw null; }
144-
public static implicit operator bool? (Azure.Core.Dynamic.JsonData json) { throw null; }
145-
public static implicit operator double? (Azure.Core.Dynamic.JsonData json) { throw null; }
146-
public static implicit operator int? (Azure.Core.Dynamic.JsonData json) { throw null; }
147-
public static implicit operator long? (Azure.Core.Dynamic.JsonData json) { throw null; }
148-
public static implicit operator float? (Azure.Core.Dynamic.JsonData json) { throw null; }
149-
public static implicit operator float (Azure.Core.Dynamic.JsonData json) { throw null; }
150-
public static implicit operator string (Azure.Core.Dynamic.JsonData json) { throw null; }
151-
public static bool operator !=(Azure.Core.Dynamic.JsonData? left, bool right) { throw null; }
152-
public static bool operator !=(Azure.Core.Dynamic.JsonData? left, double right) { throw null; }
153-
public static bool operator !=(Azure.Core.Dynamic.JsonData? left, int right) { throw null; }
154-
public static bool operator !=(Azure.Core.Dynamic.JsonData? left, long right) { throw null; }
155-
public static bool operator !=(Azure.Core.Dynamic.JsonData? left, float right) { throw null; }
156-
public static bool operator !=(Azure.Core.Dynamic.JsonData? left, string? right) { throw null; }
157-
public static bool operator !=(bool left, Azure.Core.Dynamic.JsonData? right) { throw null; }
158-
public static bool operator !=(double left, Azure.Core.Dynamic.JsonData? right) { throw null; }
159-
public static bool operator !=(int left, Azure.Core.Dynamic.JsonData? right) { throw null; }
160-
public static bool operator !=(long left, Azure.Core.Dynamic.JsonData? right) { throw null; }
161-
public static bool operator !=(float left, Azure.Core.Dynamic.JsonData? right) { throw null; }
162-
public static bool operator !=(string? left, Azure.Core.Dynamic.JsonData? right) { throw null; }
123+
internal DynamicJson() { }
124+
public static implicit operator bool (Azure.Core.Dynamic.DynamicJson value) { throw null; }
125+
public static implicit operator double (Azure.Core.Dynamic.DynamicJson value) { throw null; }
126+
public static implicit operator int (Azure.Core.Dynamic.DynamicJson value) { throw null; }
127+
public static implicit operator long (Azure.Core.Dynamic.DynamicJson value) { throw null; }
128+
public static implicit operator bool? (Azure.Core.Dynamic.DynamicJson value) { throw null; }
129+
public static implicit operator double? (Azure.Core.Dynamic.DynamicJson value) { throw null; }
130+
public static implicit operator int? (Azure.Core.Dynamic.DynamicJson value) { throw null; }
131+
public static implicit operator long? (Azure.Core.Dynamic.DynamicJson value) { throw null; }
132+
public static implicit operator float? (Azure.Core.Dynamic.DynamicJson value) { throw null; }
133+
public static implicit operator float (Azure.Core.Dynamic.DynamicJson value) { throw null; }
134+
public static implicit operator string (Azure.Core.Dynamic.DynamicJson value) { throw null; }
163135
System.Dynamic.DynamicMetaObject System.Dynamic.IDynamicMetaObjectProvider.GetMetaObject(System.Linq.Expressions.Expression parameter) { throw null; }
164136
public override string ToString() { throw null; }
137+
[System.Diagnostics.DebuggerDisplayAttribute("{Current,nq}")]
138+
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
139+
public partial struct ArrayEnumerator : System.Collections.Generic.IEnumerable<Azure.Core.Dynamic.DynamicJson>, System.Collections.Generic.IEnumerator<Azure.Core.Dynamic.DynamicJson>, System.Collections.IEnumerable, System.Collections.IEnumerator, System.IDisposable
140+
{
141+
private object _dummy;
142+
private int _dummyPrimitive;
143+
public Azure.Core.Dynamic.DynamicJson Current { get { throw null; } }
144+
object System.Collections.IEnumerator.Current { get { throw null; } }
145+
public void Dispose() { }
146+
public Azure.Core.Dynamic.DynamicJson.ArrayEnumerator GetEnumerator() { throw null; }
147+
public bool MoveNext() { throw null; }
148+
public void Reset() { }
149+
System.Collections.Generic.IEnumerator<Azure.Core.Dynamic.DynamicJson> System.Collections.Generic.IEnumerable<Azure.Core.Dynamic.DynamicJson>.GetEnumerator() { throw null; }
150+
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; }
151+
}
152+
}
153+
public partial class MutableJsonDocument
154+
{
155+
internal MutableJsonDocument() { }
156+
public Azure.Core.Dynamic.MutableJsonElement RootElement { get { throw null; } }
157+
public static Azure.Core.Dynamic.MutableJsonDocument Parse(System.BinaryData utf8Json) { throw null; }
158+
public static Azure.Core.Dynamic.MutableJsonDocument Parse(string json) { throw null; }
159+
public void WriteTo(System.IO.Stream stream, System.Buffers.StandardFormat format = default(System.Buffers.StandardFormat)) { }
160+
}
161+
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
162+
public partial struct MutableJsonElement
163+
{
164+
private object _dummy;
165+
private int _dummyPrimitive;
166+
public System.Text.Json.JsonValueKind ValueKind { get { throw null; } }
167+
public Azure.Core.Dynamic.MutableJsonElement.ArrayEnumerator EnumerateArray() { throw null; }
168+
public bool GetBoolean() { throw null; }
169+
public double GetDouble() { throw null; }
170+
public int GetInt32() { throw null; }
171+
public long GetInt64() { throw null; }
172+
public Azure.Core.Dynamic.MutableJsonElement GetProperty(string name) { throw null; }
173+
public float GetSingle() { throw null; }
174+
public string? GetString() { throw null; }
175+
public void RemoveProperty(string name) { }
176+
public void Set(Azure.Core.Dynamic.MutableJsonElement value) { }
177+
public void Set(bool value) { }
178+
public void Set(double value) { }
179+
public void Set(int value) { }
180+
public void Set(long value) { }
181+
public void Set(object value) { }
182+
public void Set(float value) { }
183+
public void Set(string value) { }
184+
public Azure.Core.Dynamic.MutableJsonElement SetProperty(string name, object value) { throw null; }
185+
public override string ToString() { throw null; }
186+
public bool TryGetProperty(string name, out Azure.Core.Dynamic.MutableJsonElement value) { throw null; }
187+
[System.Diagnostics.DebuggerDisplayAttribute("{Current,nq}")]
188+
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
189+
public partial struct ArrayEnumerator : System.Collections.Generic.IEnumerable<Azure.Core.Dynamic.MutableJsonElement>, System.Collections.Generic.IEnumerator<Azure.Core.Dynamic.MutableJsonElement>, System.Collections.IEnumerable, System.Collections.IEnumerator, System.IDisposable
190+
{
191+
private object _dummy;
192+
private int _dummyPrimitive;
193+
public Azure.Core.Dynamic.MutableJsonElement Current { get { throw null; } }
194+
object System.Collections.IEnumerator.Current { get { throw null; } }
195+
public void Dispose() { }
196+
public Azure.Core.Dynamic.MutableJsonElement.ArrayEnumerator GetEnumerator() { throw null; }
197+
public bool MoveNext() { throw null; }
198+
public void Reset() { }
199+
System.Collections.Generic.IEnumerator<Azure.Core.Dynamic.MutableJsonElement> System.Collections.Generic.IEnumerable<Azure.Core.Dynamic.MutableJsonElement>.GetEnumerator() { throw null; }
200+
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; }
201+
}
165202
}
166203
}

sdk/core/Azure.Core.Experimental/src/Azure.Core.Experimental.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>Experimental types that might eventually move to Azure.Core</Description>
44
<AssemblyTitle>Microsoft Azure Client Pipeline Experimental Extensions</AssemblyTitle>
55
<Version>0.1.0-preview.24</Version>
66
<PackageTags>Microsoft Azure Client Pipeline</PackageTags>
77
<Nullable>enable</Nullable>
8-
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
8+
<TargetFrameworks>$(RequiredTargetFrameworks);net461;net6.0</TargetFrameworks>
99
<NoWarn>$(NoWarn);AZC0001;AZC0012</NoWarn>
1010
<IncludeOperationsSharedSource>true</IncludeOperationsSharedSource>
1111
</PropertyGroup>

sdk/core/Azure.Core.Experimental/src/BinaryDataExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static class BinaryDataExtensions
1515
/// </summary>
1616
public static dynamic ToDynamic(this BinaryData data)
1717
{
18-
return JsonData.Parse(data);
18+
return new DynamicJson(MutableJsonDocument.Parse(data).RootElement);
1919
}
2020
}
2121
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
using System.Text.Json;
4+
using System.IO;
55

66
namespace Azure.Core.Dynamic
77
{
@@ -14,15 +14,15 @@ namespace Azure.Core.Dynamic
1414
public abstract class DynamicData
1515
{
1616
/// <summary>
17-
/// Writes the data to the provided writer as a JSON value.
17+
/// Writes the data to the provided stream.
1818
/// </summary>
19-
/// <param name="writer">The writer to which to write the document.</param>
19+
/// <param name="stream">The stream to which to write the document.</param>
2020
/// <param name="data">The dynamic data value to write.</param>
21-
public static void WriteTo(Utf8JsonWriter writer, DynamicData data)
21+
public static void WriteTo(Stream stream, DynamicData data)
2222
{
23-
data.WriteTo(writer);
23+
data.WriteTo(stream);
2424
}
2525

26-
internal abstract void WriteTo(Utf8JsonWriter writer);
26+
internal abstract void WriteTo(Stream stream);
2727
}
2828
}

0 commit comments

Comments
 (0)