digitalmars.D.learn - Help on asdf json module
- Vino (25/25) Oct 24 2020 Hi All,
- ikod (6/17) Oct 25 2020 Maybe library author didn't implement opApply, so you have to
- Sebastiaan Koppe (26/33) Oct 25 2020 The API's are different. You can't just expect one to work with
- 9il (5/13) Oct 25 2020 Hi Vino,
Hi All,
Currently we are testing various json module such as
"std.json, std_data_json, vibe.data.json and asdf", the below
code works perfectely while use "std_data_json or
vibe.data.json" but not working as expected when we use "asdf"
and throwing the below error, hence request your help on the same.
Error:
source/app.d(12,2): Error: invalid foreach aggregate
jv.opIndex(["items"][]), define opApply(), range primitives, or
use .tupleof
Code:
import std.stdio: writeln;
import vibe.data.json; // works
//import asdf; // Does not work.
import std.conv: to;
void main()
{
string apidata =
`{"items":[{"name":"T10","hostname":"test1","type":[{"typeValue":"TD,dev dev.com,DEVt"},{"typeno":"000"}]},{"name":"T11","hostname":"test2","type":[{"typeValue":"TF,qas qas.com,QAS"},{"typeno":"100"}]},{"name":"T11","hostname":"test3","type":[{"typeValue":"TP,prd prd.com,PRD"},{"typeno":"101"}]}]}`;
auto jv = parseJson(apidata);
foreach(j; jv["items"]){
writeln(j["name"].toString());
}
}
Vino.B
Oct 24 2020
On Sunday, 25 October 2020 at 06:05:27 UTC, Vino wrote:
Hi All,
Currently we are testing various json module such as
"std.json, std_data_json, vibe.data.json and asdf", the below
code works perfectely while use "std_data_json or
vibe.data.json" but not working as expected when we use "asdf"
and throwing the below error, hence request your help on the
same.
Error:
source/app.d(12,2): Error: invalid foreach aggregate
jv.opIndex(["items"][]), define opApply(), range primitives, or
use .tupleof
Maybe library author didn't implement opApply, so you have to
iterate using range primitives empty, front, popFront, or handle
data using algorithms.
You can check examples on the project page
https://github.com/libmir/asdf
Oct 25 2020
On Sunday, 25 October 2020 at 06:05:27 UTC, Vino wrote:
Hi All,
Currently we are testing various json module such as
"std.json, std_data_json, vibe.data.json and asdf", the below
code works perfectely while use "std_data_json or
vibe.data.json" but not working as expected when we use "asdf"
and throwing the below error, hence request your help on the
same.
The API's are different. You can't just expect one to work with
the api of the other.
Asdf is a bit more strict in that you have to specify whether you
intent to iterate a json object (with `.byKeyValue()`) or a json
array (with `.byElement()`).
Also the way you fetch values out of it is different. There is
`.get!T(default)` that requires a default value in case it is
empty (undefined), or `cast(T)field` if you assume there is a
value and want an exception throw if not.
----
/+dub.sdl:
dependency "asdf" version="~>0.6.6"
+/
import std.stdio: writeln;
import asdf;
import std.conv: to;
void main()
{
string apidata =
`{"items":[{"name":"T10","hostname":"test1","type":[{"typeValue":"TD,dev dev.com,DEVt"},{"typeno":"000"}]},{"name":"T11","hostname":"test2","type":[{"typeValue":"TF,qas qas.com,QAS"},{"typeno":"100"}]},{"name":"T11","hostname":"test3","type":[{"typeValue":"TP,prd prd.com,PRD"},{"typeno":"101"}]}]}`;
auto jv = parseJson(apidata);
foreach(j; jv["items"].byElement()){
writeln(j["name"].get!string("default"));
}
}
Oct 25 2020
On Sunday, 25 October 2020 at 06:05:27 UTC, Vino wrote:
Hi All,
Currently we are testing various json module such as
"std.json, std_data_json, vibe.data.json and asdf", the below
code works perfectely while use "std_data_json or
vibe.data.json" but not working as expected when we use "asdf"
and throwing the below error, hence request your help on the
same.
[...]
Hi Vino,
byElement should be used here for ASDF.
foreach(j; jv["items"].byElement)
Oct 25 2020
On Sunday, 25 October 2020 at 08:22:06 UTC, 9il wrote:On Sunday, 25 October 2020 at 06:05:27 UTC, Vino wrote:Hi All, Thank you for your help, and now need your suggestion as the below code is working and it also prints an additional value which is not expected, so request your help on how to avoid the additional value, Code: import std.stdio: writeln; import asdf: parseJson; import std.conv: to; import std.range: takeExactly; import std.array: split; void main() { string apidata = `{"items": [ {"name":"T01","hostname":"test01","pool":"Development","type": [{"Value":"D,dev dev.com,DEV"},{"Value":"000"}]}, {"name":"T02","hostname":"test02","pool":"Quality","type": [{"Value":"Q,qas qas.com,QAS"},{"Value":"100"}]}, {"name":"T03","hostname":"test03","pool":"Production","type": [{"Value":"P,prd prd.com,PRD"},{"Value":"100"}]} ] }`; auto jv = parseJson(apidata); foreach(j; jv["items"].byElement()){ foreach(i; j["type"].byElement().takeExactly(1)) { writeln(i["Value"].get!string("default").split(",")[1]); } } } Output: dev dev.com qas qas.com prd prd.com [] // additional value which is not expected From, Vino.BHi All, Currently we are testing various json module such as "std.json, std_data_json, vibe.data.json and asdf", the below code works perfectely while use "std_data_json or vibe.data.json" but not working as expected when we use "asdf" and throwing the below error, hence request your help on the same. [...]Hi Vino, byElement should be used here for ASDF. foreach(j; jv["items"].byElement)
Oct 26 2020
On Monday, 26 October 2020 at 09:43:44 UTC, Vino wrote:On Sunday, 25 October 2020 at 08:22:06 UTC, 9il wrote:Hi All, Sorry, I was able to find the issue as there was an additional writeln in the code hence the additional output was printed. From, Vino.B[...]Hi All, Thank you for your help, and now need your suggestion as the below code is working and it also prints an additional value which is not expected, so request your help on how to avoid the additional value, [...]
Oct 26 2020









ikod <igor.khasilev gmail.com> 