fsprojects / FSharp.Data

F# Data: Library for Data Access
https://fsprojects.github.io/FSharp.Data
Other
816 stars 288 forks source link

How to Mutate/useLenses with JsonProvider (or JDocument or JsonValue) #1382

Open jkone27 opened 3 years ago

jkone27 commented 3 years ago

Hello! Someone can show an example of how to use Lenses with FSharp.Data.JsonValue or With a Json provider generated type? using one of the 2 libraries.. (or alternative approaches) https://fsprojects.github.io/Fleece/reference/fleece-fsharpdata.html , https://xyncro.tech/aether/guides/lenses.html still cannot fully grasp why JsonValue is get only in this library... making it also optionally settable would ease the usage of Json provider to me. especially for scripts...

could this be done without impacting usage or with any drawbacks?

Thanks! have a lovely day

jkone27 commented 3 years ago

@here I have developed this extension library, if anyone is interested, let me know what you think, hope is OK 👍 ✅, license is MIT.

https://jkone27-3876.medium.com/fsharp-data-mutator-66550bb6a2cc

FSharp.Data is amazing! go type providers, go !

jkone27 commented 2 years ago

would it be possible to add something similar to the main codebase (to allow JsonValue object copy with different properties, similar to what you can do in records with the with keyword ?


let myJson = MyJsonProvidedType.Parse """ {  "name" : "test" } """

myJson.Name // "test"

let myJson2 = { myJson with myJson.Name = "new" }

myJson2.Name // "new"

//or with some custom CE maybe ? (since they are not records)

let myJson2 = json { 
   let! myJson.name = "new"
   return myJson 
   }
jkone27 commented 2 years ago

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-use-dom-utf8jsonreader-utf8jsonwriter?pivots=dotnet-6-0#use-jsonnode