This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Jesse Phillips
JSON is a data format that uses Javascript notation to layout the structure of the data. But Javascript isn't the only language that can represent these structures in code.
// Javascript
{
"firstitem" : "first data",
"list" : [
"bold",
"italics"
]
}
I'm utilizing the two main structures, generally referred to as 'object' {}
and 'list' []
because that is how Javascript talks of them. Most languages can represent these in one or more ways. These structures don't produce JSON, but libraries (beyond this article) can make the translation.
// Lua
{
firstitem = "first data",
list = {
"bold",
"italics"
}
}
Lua is almost the same with just object {}
notation even for lists. The equal rather than colon is used for association. We also don't quote the field names, though Lua does allow spaces like JSON, ["first item"] = "first data"
.
// Python
{
'firstitem' : 'first data',
'list' : [
'bold',
'italics'
]
}
Python looks really close, but string literals are single not double quote. We can also use tuples to build your list.
// Python
{
'firstitem' : 'first data',
'list' : (
'bold',
'italics'
)
}
This makes the list syntax different, but you'll get the needed output.
// c#
public class Data
{
public string firstitem { get; set; }
public List<string> list { get; set; }
}
C# is a statically typed language so we define a type that matches the structure. However C# has many different features and we can get away with much more.
// c#
new {
firstitem = "first data",
list = new List<string> {
"bold",
"italics",
}
}
// c#
Dictionary<string, object> data = new {
{ "firstitem", "first data" },
{ "list", new List<string> {
"bold", "italics",
}},
};
This one is a cheat. The specification of object
just places the data in to the libraries "general" representation, for example Newtonsoft's Json library would use a JObject, JArray for the type you could cast to.
// D
public class Data
{
public string firstitem;
public string[] list;
}
// D
new class {
auto firstitem = "first data";
auto list = [
"bold",
"italics"
];
}
// D
Variant[string] data = [
"firstitem" : Variant("first data"),
"list" : Variant([
"bold",
"italics",
])
];
Unlike in C# where all types have an object
representation, there is not parent for all types with Auto-Boxing. D does however provide a type, Variant
, which can hold other types and that needs to be explicitly created.
This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Jesse Phillips
Jesse Phillips | Sciencx (2022-10-19T17:43:31+00:00) JSON in Different Languages. Retrieved from https://www.scien.cx/2022/10/19/json-in-different-languages/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.