Coder Social home page Coder Social logo

Comments (6)

elgonzo avatar elgonzo commented on May 25, 2024 2

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.)

from newtonsoft.json.

bartelink avatar bartelink commented on May 25, 2024 2

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)

from newtonsoft.json.

64J0 avatar 64J0 commented on May 25, 2024 1

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.

from newtonsoft.json.

sungam3r avatar sungam3r commented on May 25, 2024 1

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

from newtonsoft.json.

sungam3r avatar sungam3r commented on May 25, 2024

But you'd make a champion maintainer

100% agree

from newtonsoft.json.

bartelink avatar bartelink commented on May 25, 2024

100% agree

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

from newtonsoft.json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.