Re: Help on asdf json module

2020-10-26 Thread Vino via Digitalmars-d-learn

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,

  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,


[...]


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


Re: Help on asdf json module

2020-10-26 Thread Vino via Digitalmars-d-learn

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,

   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)

http://asdf.libmir.org/asdf_asdf.html#.Asdf.byElement


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,d...@dev.com,DEV"},{"Value":"000"}]},
{"name":"T02","hostname":"test02","pool":"Quality","type":
[{"Value":"Q,q...@qas.com,QAS"},{"Value":"100"}]},
{"name":"T03","hostname":"test03","pool":"Production","type":
[{"Value":"P,p...@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:
d...@dev.com
q...@qas.com
p...@prd.com
[]   // additional value which is not expected

From,
Vino.B



Re: Help on asdf json module

2020-10-25 Thread 9il via Digitalmars-d-learn

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)

http://asdf.libmir.org/asdf_asdf.html#.Asdf.byElement


Re: Help on asdf json module

2020-10-25 Thread Sebastiaan Koppe via Digitalmars-d-learn

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,d...@dev.com,DEVt"},{"typeno":"000"}]},{"name":"T11","hostname":"test2","type":[{"typeValue":"TF,q...@qas.com,QAS"},{"typeno":"100"}]},{"name":"T11","hostname":"test3","type":[{"typeValue":"TP,p...@prd.com,PRD"},{"typeno":"101"}]}]}`;

 auto jv = parseJson(apidata);
 foreach(j; jv["items"].byElement()){
writeln(j["name"].get!string("default"));
 }
}


Re: Help on asdf json module

2020-10-25 Thread ikod via Digitalmars-d-learn

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


Help on asdf json module

2020-10-24 Thread Vino via Digitalmars-d-learn

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,d...@dev.com,DEVt"},{"typeno":"000"}]},{"name":"T11","hostname":"test2","type":[{"typeValue":"TF,q...@qas.com,QAS"},{"typeno":"100"}]},{"name":"T11","hostname":"test3","type":[{"typeValue":"TP,p...@prd.com,PRD"},{"typeno":"101"}]}]}`;

 auto jv = parseJson(apidata);
 foreach(j; jv["items"]){
writeln(j["name"].toString());
 }
}

Vino.B