Is encoding/json Marshal a JSON alphabetically by default? [closed] - json

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 months ago.
Improve this question
func sort(input string) string {
var data interface{}
_ = json.Unmarshal([]byte(input), &data)
ret, _ := json.Marshal(data)
return string(ret)
}
just like code above, if i Unmarshal a json string into interface, then Marshal it to json string, is this json ret sort by alphabetically default?
seems like encoding/json Marshal will sort keys by alphabetically default, but is there any doc or article prove this?
thanks

According to the functions documentation:
Map values encode as JSON objects. The map's key type must either be a string, an integer type, or implement encoding.TextMarshaler. The map keys are sorted and used as JSON object keys by applying the following rules, subject to the UTF-8 coercion described for string values above:
keys of any string type are used directly
encoding.TextMarshalers are marshaled
integer keys are converted to strings

Related

Parsing a valid json string by serde will cause an error - "trailing characters" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have a Rust library and Ruby project. I call a Rust function from Ruby via FFi.
There's a function in the Rust library that receives a string and converts it into json.
Ruby side:
my_json_raw_str = {
network: {
server_address: "my_server.com"
}
}
res = send_it_to_rust(my_json_raw_str.to_json)
A function in Rust will throw an exception when parsing json string sent to it from Ruby.
An error returned from Rust:
Invalid parameters: trailing characters at line 1 column 47\nparams: [{\"network\":{\"server_address\":\"my_server.com\"}}\u0000]
Json is valid, isn't it?
serde, serde_json and serde_derive are used on Rust side.
How to fix the error and why is it caused?
Json is valid, isn't it?
Your JSON is not valid, because your FFI layer is not correct: if you look at the error it's clearly telling you that there is a trailing NUL byte in your data, meaning when bridging between C and Rust you left the trailing NUL byte from the C string.

Marshalling Struct vs Map in Golang [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Which is better in term of performance or best practice when returning result of JSON, using marshalled struct or map. I've seen some codes use map and marshal them to be returned as json, others use marshaled struct to return json response.
Example:
Use struct:
type Response struct {
A int `json:"a"`
B string `json:"b"`
}
r := Response{A:1,B:"1"}
resp, _ := json.Marshal(r)
Use map:
m := map[string]interface{}{
A: 1,
B: "1",
}
resp, _ := json.Marshal(m)
Which is better?
My though is that using Struct is better since the types of the fields are defined. If you use map then it's obviously you'll use map[string]interface{} since the values will vary. Using interfaces typed data will allocate heap memories. Since you're just returning response then it's better to use struct with defined types to reduce run time checking for the types. The performance difference is not significance tho.

How to validate two JSON objects present in a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a JSON String to validate which contains two separate objects. The string is "1A" but i want to validate it as individual objects for example: {"NumberInt":1,"LetterThing":"A"}.
In conclusion, my string is "1A" but I need to validate it as individual objects despite the fact it's in a string format.
Why do I want this? I have a minimum and maximum for the NumberInt integer value and I have a particular pattern for the LetterThing string value. For example: I do not want "5H" to validate.
If this is possible in a string format please let me know how.
Solved:
Was solved by using regex to validate on my JsonSchema i.e "pattern": "^[A-Ja-j1-4\\s]*$".
Thanks guys
You could use a regex to extract what you need from the JSON.
//obtains the number part, then you can perform operations on that number
var startingDigits = incomingString.replace( /^\D+/g, '');
You would need to PARSE the string in this case.
To parse a STRING you ITERATE over each CHARACTER in the string and then compose the needed parsed ELEMENTS.
For example in this CASE you might start looking for only DIGITS and putting them in another string. When you hit a LETTER you can CONVERT that string to an integer.
Then take the REMAINING as the 2nd part.
Finally do your VALIDATION.

All data are in parentheses instead of bracket [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
After calling data from API using, dictonary = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary. My value is always in parentheses. How do change from parentheses to bracket?
What you're seeing is just an artifact of how Xcode (really lldb) displays dictionaries when debugging.
["{key}":{value}...] will be used to display any dictionary.
({value}...) will be used to display arrays
The rest of what you see is because the coordinates array is an array(1) of array(1) of array(1) of array(2) with two values [[[[Double]]]]
Mostly you just need to learn to read the debugger output.

What is the advantage of N-Triples over CSV for storing RDF triples? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
N-Triples is a line based serialization format for an RDF graph. Each line represents the subject, predicate and object of an RDF Triple separated by whitespace and ended with a dot like:
<http://one.example/subject1> <http://one.example/predicate1> <http://one.example/object1> .
More details can be found here: http://www.w3.org/TR/n-triples/
But why is it necessary to define such a format, if one could serialize RDF Triples simply using CSV like
http://one.example/subject1, http://one.example/predicate1, http://one.example/object1
I could even easily extend to support N-Quads, N-Quints, ... using CSV. What are the advantages of N-Triples over CSV for serializing RDF triples?
Disclaimer: I'm the original editor of N-Triples and implemented it in Raptor http://librdf.org/raptor/ both the N-Triples original and the 2013 version.
There are multiple answers to this but it's basically ambiguity. CSV can't distinguish between a URI that looks like http://foo.com/ and a string http://foo.com/
In CSV
http://foo.com/,http://foo.com/,http://foo.com/
this could be a triple
(URI http://foo.com/, URI http://foo.com/, URI http://foo.com/)
or
(URI http://foo.com/, URI http://foo.com/, Literal http://foo.com/)
N-Triples adds <> and "" for distinguishing these cases