Azure / azure-sdk

This is the Azure SDK parent repository and mostly contains documentation around guidelines and policies as well as the releases for the various languages supported by the Azure SDK.
http://azure.github.io/azure-sdk
MIT License
487 stars 296 forks source link

Board Review: Azure.Core JsonPatch support #1810

Closed pakrym closed 3 years ago

pakrym commented 4 years ago

The Basics

About this client library

.NET

Champion Scenarios

Some services operate on large customer-provided JSON and would like to be able to update them without having do download/modify/upload the entire entity, using the JSON Patch instead.

The type being reviewed is a strongly typed representation of a JSON Patch document builder.

var client = new DigitalTwinClient();
var jsonPatch = new JsonPatchDocument();
jsonPatch.AppendAddRaw("/twin/events", "{ \"id\": 1, "data": [] }");
jsonPatch.AppendReplace("/twin/active", true);

await client.UpdateDigitalTwinAsync("<my-twin-id>", jsonPatch);

@alzimmermsft @drwill-ms @srnagar

lilyjma commented 4 years ago

scheduled for 10/13

johanste commented 4 years ago

jsonPatch.AppendReplace("/twin/active", "true"); => jsonPatch.AppendReplace("/twin/active", true); in order to set the value to a boolean value, I assume.

Or else, how to I change the value to a new string value? jsonPatch.AppendReplace("/twin/active", "\"true\"");?

pakrym commented 4 years ago

Sorry, the sample should've been updated to be in sync with latest API changes.

Change to a bool:

jsonPatch.AppendReplace("/twin/active", true); or 
jsonPatch.AppendReplaceRaw("/twin/active", "true");

Change to a string:

jsonPatch.AppendReplace("/twin/active", "true");
jsonPatch.AppendReplaceRaw("/twin/active", "\"true\"");
johanste commented 4 years ago

Does this mean that I have to use the *Raw methods to provide an object (in json lingo) to the API? I know that at least one of the reasons that the DT team used json-patch rather than json-merge-patch was that they needed the ability to replace items in an array/list. I'd assume that it is not a perfect experience to have to escape the string yourself...

pakrym commented 4 years ago

No, generic overloads take any value and serialize them.

MyCoolModel model = new MyCoolModel()
{
   Name = "Yes"
};

jsonPatch.AppendReplace("/twin/thing", model);
johanste commented 4 years ago

Ok - that makes sense. I guess I should have looked at the apiview link.