libmir / asdf Goto Github PK
View Code? Open in Web Editor NEWJSON library
Home Page: http://asdf.libmir.org
License: Boost Software License 1.0
JSON library
Home Page: http://asdf.libmir.org
License: Boost Software License 1.0
How could I determine the position in the JSON string where ASDF deserialisation fails?
#include "simdjson.h"
int main(void) {
simdjson::dom::parser parser;
simdjson::dom::element tweets = parser.load("twitter.json");
std::cout << tweets["search_metadata"]["count"] << " results." << std::endl;
}
So asdf code:
tweets["search_metadata", "count"];
JSONValue code:
tweets["search_metadata"]["count"].get!uint;
I tried to write the following code, but I could not figure out how to implement the Proxy.
Does anyone have any good ideas?
code:
import sumtype;
struct ObjectA {
string name;
}
struct ObjectB {
double value;
}
alias MyObject = SumType!(ObjectA, ObjectB);
struct SomeObject {
@serdeProxy!MyObjectArrayProxy MyObject[] objects;
}
struct MyObjectArrayProxy {
// ?
}
data:
{
"objects": [
{ "name": "test" },
{ "value": 1.5 }
]
}
I am trying to implement a configuration file with a strict type.
I want to find the difference between two json files of unknown structure, showing which entries have been added, changed and removed.
asdf.transform.AsdfNode
has added
and removed
and that's part of the way to doing this, but actually not very convenient. Obviously I can write code to walk the AsdfNode
tree by hand, but it seems like AsdfNode
should be more helpful here as it's a common class of tasks
Building asdf 0.7.17: building configuration [library]
../../../.dub/packages/asdf/0.7.17/asdf/source/asdf/asdf.d(76,37): Error: function `const pure nothrow @nogc @trusted AsdfSerdeException asdf.asdf.AsdfSerdeException.toMutable()` does not override any function, did you mean to override `const pure nothrow @nogc scope @trusted mir.serde.SerdeException mir.serde.SerdeException.toMutable() return`?
Error /usr/bin/dmd failed with exit code 1.
In Rust the serde
macro can rename all enum members, for example:
#[serde(rename_all = "UPPERCASE")]
pub enum LoginType {
Web = 0,
Mobile = 1,
}
The D workaround for this is to use serdeKeys
:
enum LoginType {
@serdeKeys("WEB") Web,
@serdeKeys("MOBILE") Mobile,
}
But would it be possible to add something similar in D that affects all the enum members? E.g.:
@serdeRenamed(Uppercase)
enum LoginType {
Web,
Mobile,
}
The exact syntax isn't that important to me.
Hi,
Just noted the change from 0.5.x serializedXxx => 0.6.x serdeXxx, e.g.
@serializedAs => @serdeProxy
What's the reason for this kind of change? IMO, serialized is much readable English than serde.
I googled a bit, and only find: https://github.com/serde-rs/serde
But I suppose this code base is a clean D implementation, and has nothing to do with that Rust package.
So why? If it's really needed, can you also add to the doc, e.g. also the release tag for the explanation.
Thanks.
If I add asdf to the empty dub project and run 'dub build' - it builds fine. But when I use 'dub build --combined' it gives me errors:
.dub/packages/mir-core-1.3.15/mir-core/source/mir/algebraic.d(1473,36): Error: pure
function mir.algebraic.Algebraic!opEquals!().opEquals
cannot call impure function mir.annotated.U!(Algebraic!(Ion_)).Annotated.opEquals
.dub/packages/mir-core-1.3.15/mir-core/source/mir/algebraic.d(1473,36): Error: @nogc
function mir.algebraic.Algebraic!opEquals!().opEquals
cannot call non-@nogc function mir.annotated.U!(Algebraic!(Ion_)).Annotated.opEquals
.dub/packages/mir-core-1.3.15/mir-core/source/mir/algebraic.d(1473,36): Error: function mir.annotated.U!(Algebraic!.opEquals
is not nothrow
.dub/packages/mir-core-1.3.15/mir-core/source/mir/algebraic.d(1428,10): Error: function mir.algebraic.Algebraic!(Ion_).s!().opEquals
may throw but is marked as nothrow
.dub/packages/mir-algorithm-3.18.4/mir-algorithm/source/mir/annotated.d(69,54): Error: template instance mir.algebraic.Algebraic.opEquals!()
error instantiating
.dub/packages/mir-core-1.3.15/mir-core/source/mir/internal/meta.d(411,39): instantiated from here: U!(Algebraic!.dub/packages/mir-core-1.3.15/mir-core/source/mir/algebraic.d(857,36): 12 recursive instantiations from here: s!(isVariant, This, Algebraic!(Ion_), typeof(null), bool, long, double, string, Blob, Clob, Timestamp, This[], StringMap!!(This))
.dub/packages/mir-algorithm-3.18.4/mir-algorithm/source/mir/algebraic_alias/ion.d(60,22): instantiated from here: Algebraic!(Ion_)
Is it possible to fix that? So we can use asdf in '--combined' build?
https://code.dlang.org/packages/asdf/versions only lists ~master
as a release. All the tags seem to be in mir-ion
but that's not where code.dlang.org thinks asdf lives at.
Problem: the following code produces default values on deserialization in Name
structure
Expected: actual values from json in Name
structure
Could be a compiler bug, no idea.
Compilers: DMD 2.098, LDC 1.28.0-beta1 (2.098 backend)
@serdeProxy!string
struct Name
{
// immutable causes asdf to silently swallow error "can't assign 'this' in struct with immutable members"
// comment out 'immutable' to make it work as expected
immutable string godot;
immutable string d;
this(string godotName)
{
godot = godotName;
d = godotName.dTypeName;
}
}
struct Constant
{
Name name; // <- BUG: will be default initialized instead of expected values
int value;
}
struct Enum
{
string name;
Constant[] values;
}
Enum e = json.deserialize!(Enum);
test.json:
{
"name": "Mode",
"values": [
{
"name": "MODE_ECB_ENCRYPT",
"value": 0
},
]
}
Now however if I use this Constant instead I got compile error
struct Constant
{
Name name; // case
int value;
SerdeException deserializeFromAsdf(Asdf data)
{
string val;
if (auto exc = deserializeScopedString(data, val))
return exc;
// Error: cannot modify struct instance `this` of type `Constant` because it contains `const` or `immutable` members
this = Constant(Name(val), 0 );
return null;
}
}
I have the following json node (jvm):
{
"jvm":[
{
"rules":[
{
"action":"allow",
"os":{
"arch":"x86"
}
}
],
"value":"-Xss1M"
},
"-Djava.library.path=${natives_directory}"
]
}
Here the node elements can be either a string or an object.
How do I deserialize this node?
I tried something like:
import std.sumtype;
import asdf;
struct Jvm {
SumType!(JvmArgument, string) data;
alias data this;
SerdeException deserializeFromAsdf(Asdf data) {
string val;
if (auto exc = deserializeScopedString(data, val))
return exc;
this = ???
return null;
}
}
struct JvmArgument {
@serdeKeys("rules")
Rule[] rule;
@serdeKeys("value")
string[] value;
}
struct Rule{
@serdeKeys("action")
string action;
@serdeKeys("os")
@serdeOptional
Os os;
}
I don't understand how to fill in "this" in this case.
Do you have any ideas?
Hi,
asdf fails to serialize a struct with phobos Duration in it:
/+dub.sdl:
dependency "asdf" version="~>0.5.7"
+/
import std.stdio;
import std.datetime;
import asdf;
void main()
{
A a;
string json = serializeToJsonPretty(a); // never ends
writeln("json length ", json.length);
}
struct A
{
Duration dur;
}
This errors with an OOM exception in the appender. Somehow in writes never-ending zero": {
strings to it.
Hi,
I think we need add this functionality to easily compare changed json files of different versions, or at least add the option to allow user sort the dict by keys, and save it.
e.g. in Python:
https://docs.python.org/3/library/json.html
>>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True))
{"a": 0, "b": 0, "c": 0}
In case the JSON file has values like [-75, 1.234] - how can I present it with my serialization function as [-75.0, 1.234]?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.