Tarmil / FSharp.SystemTextJson

System.Text.Json extensions for F# types
MIT License
329 stars 43 forks source link

Implemented ability to deserialize discriminated unions regardless of union tag position #95

Closed xperiandri closed 3 years ago

xperiandri commented 3 years ago

I create a snapshot of a reader (a copy of structure) and parse it into a JsonDocument (which is a lazy operation) and do a union case lookup. I have not added new tests but played with modification of existing (not committed) so add tests as appropriate.

Fixes #93

xperiandri commented 3 years ago

Unfortunately, I was not managed to design changes that would allow doing ValueEquals because readExternalTag requires reading a field name. But all the other cases operate with JsonElement and its value. Hence I had to get a string instance.

xperiandri commented 3 years ago

@Tarmil could you have a look?

xperiandri commented 3 years ago

@Tarmil

Tarmil commented 3 years ago

I've been pretty busy this week but I'll take a look this weekend.

Tarmil commented 3 years ago

This looks pretty good! I was unsure whether saving and restoring a Utf8JsonReader like this would work, but it seems officially supported, so that's nice.

I added some tests, and fixed an issue in the AdjacentTag case. I'll definitely add a flag to determine whether to use it or not, because despite the low allocation we're still re-parsing the object, and some people may want to ensure they don't do that. But I think I'll make it enabled by default.

xperiandri commented 3 years ago

Cool! Thanks!

xperiandri commented 3 years ago

@Tarmil is there something left preventing this to be merged?

Tarmil commented 3 years ago

Just my schedule 🙂 I'll add the flag and release it now.