Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JToken parse is returning empty seqs #2901

Closed
64J0 opened this issue Sep 20, 2023 · 6 comments
Closed

JToken parse is returning empty seqs #2901

64J0 opened this issue Sep 20, 2023 · 6 comments

Comments

@64J0
Copy link

64J0 commented Sep 20, 2023

Hello, hope you're good. I'm trying to parse a stringfied JSON value, but the return is composed of empty seqs only. I'm using F# in .NET 7, btw.

Source/destination types

// Put the types you are serializing or deserializing here

Source/destination JSON

{
    "ParamA": "1",
    "ParamB": "artifact-00-8",
    "ParamC": "2022-09-01"
}

Expected behavior

To show the seqs with their content there.

Actual behavior

It's showing this:

m: seq [seq [seq []]; seq [seq []]; seq [seq []]]

Steps to reproduce

#r "nuget: Newtonsoft.Json, 13.0.3"

open Newtonsoft.Json

let jsonData = """{
    "ParamA": "1",
    "ParamB": "artifact-00-8",
    "ParamC": "2022-09-01"
}"""

let m = Linq.JToken.Parse(jsonData)

printfn "m: %A" (m)
@elgonzo
Copy link

elgonzo commented Sep 21, 2023

I am not the author of the library, and i can't really advise on what best to do because i am not really a F# programmer. But %A is unsuitable for outputting the content of JToken's, and i explain below why that is. It's not so much a bug but a consequence of the design decisions made when the Newtonsoft.Json.Linq stuff was being developed long, long time ago.

You might try using %s or %O instead of %A (see @bartelink's comment below) , resulting in the JToken producing a json string representative of the content of that JToken.


So, why do you get the observed output with %A.

JToken implements IEnumerable<JToken>, with the enumerated items being the children JToken's of that JToken instance. What the children are specifically depends on the concrete type of the JToken instance (like JObject, JArray, JProperty, JValue, ...)

The %A formatting will output JToken instances as enumerables, because of JToken implementing IEnumerable<JToken>. Essentially, %A will output the children JToken's of a given JToken instance, and does so recursively. And here's why you don't see neither the property names nor the values of the properties:

You parse a json object, resulting in an JObject instance. This is the outermost seq [ ... ] you see. The children of this JObject instance are the json object's properties (and their values) represented by JProperty instances. Each of the three JProperty instances of your JObject correlates with one of the three seq [seq []] in the %A output.

JProperty, by virtue of being a JToken, is also IEnumerable<JToken>. The only child item enumerated by JProperty is its value. Note that the property name itself is not a JToken and is not part of the children enumeration of a JProperty instance. Hence why there is no property name appearing in seq [seq []]. (The name of the json property is accessible through JProperty.Name.)

The values of the json properties in your example json are simple json string values and therefore will be represented by JValue instances. And JValue is also a JToken, so JValue is also an IEnumerable<JToken>. But a JValue instance has no children, it is therefore an empty IEnumerable<JToken> sequence. Hence why you see only the empty sequence seq [] appearing inside the seq [seq []]. (The value represented by an JValue instance is accessible through JValue.Value.)

@bartelink
Copy link

I am not the author of the library,

But you'd make a champion maintainer :P

You might try using %s instead of %A,

one tiny correction to an otherwise excellent answer: %O is the F# format spec that's used to trigger formatting values via .ToString(); %s will insist on the expression value being implicitly convertible to a String (in general, you're best off using %s and then sanity checking the correct way to render a value - ToString() can sometimes do the right thing, but esp for things that parse into trees, a one size fits all rendering may not even be what you want in any case)

I'd suggest closing this issue as we're more in F# and/or newtonsoft usage question territory than an actual issue with the library that anyone can do anything specific to resolve.

(I'd also suggest looking at the FSharp.SystemTextJson and System.Text.Json libraries if you're building something new, unless you're in a system that has standardised on Newtonsoft.Json)

@64J0
Copy link
Author

64J0 commented Sep 21, 2023

Thank you for the great replies and insights @elgonzo and @bartelink.

I'd suggest closing this issue as we're more in F# and/or newtonsoft usage question territory than an actual issue with the library that anyone can do anything specific to resolve.

Agreed, I'll close this issue now.

@64J0 64J0 closed this as completed Sep 21, 2023
@sungam3r
Copy link

But you'd make a champion maintainer

100% agree

@bartelink
Copy link

100% agree

That's easy to say for a future co-maintainer :P

@sungam3r
Copy link

Watching activity here in repo for several years I strongly believe that Json.Net itself has no future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants