Closed 64J0 closed 11 months ago
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.)
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
)
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.
But you'd make a champion maintainer
100% agree
100% agree
That's easy to say for a future co-maintainer :P
Watching activity here in repo for several years I strongly believe that Json.Net itself has no future.
Hello, hope you're good. I'm trying to parse a stringfied JSON value, but the return is composed of empty
seq
s only. I'm using F# in .NET 7, btw.Source/destination types
Source/destination JSON
Expected behavior
To show the
seq
s with their content there.Actual behavior
It's showing this:
Steps to reproduce