Comments (6)
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.
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.
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.
Watching activity here in repo for several years I strongly believe that Json.Net itself has no future.
from newtonsoft.json.
But you'd make a champion maintainer
100% agree
from newtonsoft.json.
100% agree
That's easy to say for a future co-maintainer :P
from newtonsoft.json.
Related Issues (20)
- ParseReadString OOM HOT 1
- Filter where Array contains value using JSONPATH
- jsonpath arithmetic operators support HOT 1
- Add support for the four `System.Text.Json` types HOT 1
- Can't deserialize object just serialized. HOT 2
- Serialize/Deserialize not working in Release build HOT 7
- Valid JsonPath expression `$.*` on Json Array results in exception HOT 1
- Crash on android aarch64 (Using Unity il2cpp) HOT 1
- Read JObject Property: System.Security.VerificationException: Operation could destabilize the runtime HOT 1
- array.length jsonpath for the selectToken method returned null instead of count HOT 1
- String Property With Math Function Not Deserialized Correctly HOT 1
- The C# `required` modifier should trigger `Required.Always` behaviour HOT 3
- Provide a centralized way to disable ExpressionValueProvider or fix DynamicCodeGeneration check HOT 6
- JsonTextWriter produces invalid JSON when Unicode char \u201d present in string HOT 1
- Unexpected exceptions during deserialization due to missing constructor name HOT 1
- Non-Null default value leads to invalid cache value being assigned HOT 2
- PopulateObject won't throw Exception on invalid JSON
- Inquiry Regarding Privacy Policy in Newtonsoft.Json HOT 2
- Multiple JsonSerializerSettings are sharing same CamelCasePropertyNamesContractResolver cache HOT 1
- Deserialize decimal same code results not match HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from newtonsoft.json.