Re: JSON Encoding and Decoding

2018-02-28 Thread Aditya Xavier
Hi Mynewt Team,

I have been trying to decode JSON using the provided json decoding library.

This particular library seems quite similar to mJSON, however there are 
difference regarding json_read_object etc.

Also unlike mJSON, there is a requirement to write few more methods :-

char jbuf_read_next(struct json_buffer *jb);
char jbuf_read_prev(struct json_buffer *jb);
int jbuf_readn(struct json_buffer *jb, char *buf, int size);
int write(void *buf, char* data, int len);
void buf_init(struct jbuf *ptjb, char *string);

At least thats what I gathered as per the test example. (Am I correct in this 
thought ?)

However, am unable to get the following code working.
/*---
#define MAXCHANNELS 72

long long int PRN[MAXCHANNELS];
long long int elevation[MAXCHANNELS];
long long int azimuth[MAXCHANNELS];

int visible;
struct jbuf tjb;

const struct json_attr_t sat_attrs[] = {
{   .attribute = "PRN",
.type = t_integer,
.addr.integer = PRN
},
{   .attribute = "el", 
.type = t_integer,
.addr.integer = elevation
},
{
.attribute = "az", 
.type = t_integer,
.addr.integer = azimuth
},
{NULL},
};

const struct json_attr_t json_attrs_sky[] = {
{   
.attribute = "class",
.type = t_check,
.dflt.check = "SKY"
},
{
.attribute = "satellites", 
.type = t_array,
.addr.array = {
.element_type = t_structobject,
.arr.objects.subtype=sat_attrs,
.maxlen = MAXCHANNELS,
.count = 
}
},
{NULL},
};

int fetch_map(const char *map){

//  int i;

buf_init(,(char *) map);
console_printf("Buffer Initiated\n");

json_read_object(_buf, json_attrs_sky);

console_printf("JSON Read %d!\n", visible);

// for (i = 0; i < visible; i++){
console_printf("PRN = %lld, elevation = %lld, azimuth = %lld\n", 
PRN[0], elevation[0], azimuth[0]);
console_printf("PRN = %lld, elevation = %lld, azimuth = %lld\n", 
PRN[1], elevation[1], azimuth[1]);
// }
printf(“Completed\n");
return 1;

}
*/

Calling this method using 

fetch_map(R"=({"class":"SKY","satellites":[{"PRN":10,"el":45,"az":196,"used":true},{"PRN":29,"el":67,"az":310,"used":true}]})=“);

Am getting the following response :-

01 Buffer Initiated
02 JSON Read 0!
03 PRN = 10, elevation = 45, azimuth = 196
04 PRN = 0, elevation = 0, azimuth = 0
Completed

Which means its not populating the count value in the array as it should. 

It is also quite possible am completely wrong about it :)

Thanks,
Aditya Xavier.


> On 28-Feb-2018, at 10:49 PM, Aditya Xavier  wrote:
> 
> I have been trying to use the same example as is; assuming that apache was 
> also utilizing mJSON.
> 
> However, it seems there are quite some differences from that library. For 
> e.g. 
> 
> .addr.integer = PRN  is no longer valid.
> 
> It is now 
> .arr.integers.store = PRN
> 
> And json_read_object requires a struct json_buffer *jb instead of a char 
> array.
> 
> This struct has to be populated using user method etc. There are multiple 
> differences from the MicroJSON implementation.
> 
> Isn’t there some good documentation on this ?
> 
> 
>> On 28-Feb-2018, at 10:39 PM, Christopher Collins  wrote:
>> 
>> Hi Aditya,
>> 
>> On Wed, Feb 28, 2018 at 06:35:04PM +0530, Aditya Xavier wrote:
>>> Thanks got Encoding working for the required JSON.
>>> 
>>> Any pointers on how to get the decoding working ?
>>> 
>>> From the example 
>>> https://github.com/apache/mynewt-core/blob/master/encoding/json/test/src/testcases/json_simple_decode.c
>>>  
>>> 
>>> 
>>> Am able to decode upto name2 only.
>>> 
>>> {“name1": 1,”name2": “value2”, “name3”: [{“name4”: 2, “name5”: 3}]};
>>> 
>>> Please let me know the Structure for Array of objects.
>> 
>> Example 2 in the microjson document
>> (http://www.catb.org/~esr/microjson/microjson.html) does something
>> similar, so I would take a look at that.  This example is under the
>> "Compound Value Types" heading.
>> 
>> Chris
> 



Re: JSON Encoding and Decoding

2018-02-28 Thread Aditya Xavier
I have been trying to use the same example as is; assuming that apache was also 
utilizing mJSON.

However, it seems there are quite some differences from that library. For e.g. 

.addr.integer = PRN  is no longer valid.

It is now 
.arr.integers.store = PRN

And json_read_object requires a struct json_buffer *jb instead of a char array.

This struct has to be populated using user method etc. There are multiple 
differences from the MicroJSON implementation.

Isn’t there some good documentation on this ?


> On 28-Feb-2018, at 10:39 PM, Christopher Collins  wrote:
> 
> Hi Aditya,
> 
> On Wed, Feb 28, 2018 at 06:35:04PM +0530, Aditya Xavier wrote:
>> Thanks got Encoding working for the required JSON.
>> 
>> Any pointers on how to get the decoding working ?
>> 
>> From the example 
>> https://github.com/apache/mynewt-core/blob/master/encoding/json/test/src/testcases/json_simple_decode.c
>>  
>> 
>> 
>> Am able to decode upto name2 only.
>> 
>> {“name1": 1,”name2": “value2”, “name3”: [{“name4”: 2, “name5”: 3}]};
>> 
>> Please let me know the Structure for Array of objects.
> 
> Example 2 in the microjson document
> (http://www.catb.org/~esr/microjson/microjson.html) does something
> similar, so I would take a look at that.  This example is under the
> "Compound Value Types" heading.
> 
> Chris



Re: JSON Encoding and Decoding

2018-02-28 Thread Christopher Collins
Hi Aditya,

On Wed, Feb 28, 2018 at 06:35:04PM +0530, Aditya Xavier wrote:
> Thanks got Encoding working for the required JSON.
> 
> Any pointers on how to get the decoding working ?
> 
> From the example 
> https://github.com/apache/mynewt-core/blob/master/encoding/json/test/src/testcases/json_simple_decode.c
>  
> 
> 
> Am able to decode upto name2 only.
> 
> {“name1": 1,”name2": “value2”, “name3”: [{“name4”: 2, “name5”: 3}]};
> 
> Please let me know the Structure for Array of objects.

Example 2 in the microjson document
(http://www.catb.org/~esr/microjson/microjson.html) does something
similar, so I would take a look at that.  This example is under the
"Compound Value Types" heading.

Chris


Re: JSON Encoding and Decoding

2018-02-28 Thread Aditya Xavier
Thanks got Encoding working for the required JSON.

Any pointers on how to get the decoding working ?

From the example 
https://github.com/apache/mynewt-core/blob/master/encoding/json/test/src/testcases/json_simple_decode.c
 


Am able to decode upto name2 only.

{“name1": 1,”name2": “value2”, “name3”: [{“name4”: 2, “name5”: 3}]};

Please let me know the Structure for Array of objects.


struct json_attr_t test_attr[3] = {
[0] = {
.attribute = “name1",
.type = t_integer,
.addr.integer = _val,
.nodefault = true
},
[1] = {
.attribute = “name2",
.type = t_string,
.addr.string = string1,
.nodefault = true,
.len = sizeof(string1)
},
[2] = {
.attribute = “name3",
.type = t_array,
.addr.array = {
.element_type = t_integer,
.arr.integers.store = intarr,
.maxlen = sizeof intarr / sizeof intarr[0],
.count = _count,
},
.nodefault = true,
.len = sizeof(intarr)
},
[6] = {
.attribute = NULL
}
};


> On 28-Feb-2018, at 2:05 PM, marko kiiskila  wrote:
> 
> 
> 
>> On Feb 28, 2018, at 10:10 AM, Aditya Xavier  wrote:
>> 
>> Yes, that was derp from my end.
>> 
>> Any clue on how to encode the JSON structure I mentioned ?
>> 
>> Is this method correct ?
> 
> You need to initialize the encoder; it needs to be told where to write
> the encoded data. Specifically, you need to at least fill in je_write
> function pointer. This is who’ll get the stream of encoded data.
> 
> Take a look at unit test @ 
> encoding/json/test/src/testcases/json_simple_encode.c
> 
> Otherwise it looks pretty good, based on my cursory inspection.
> 
>> 
>>  struct json_encoder *encoder, *module;
>>  struct json_value data;
>> 
>>  memset(, 0, sizeof(encoder));
>>  memset(, 0, sizeof(module));
>> 
>>  json_encode_object_start(encoder);
>> 
>>  JSON_VALUE_INT(, 1);
>>  json_encode_object_entry(encoder, “name1", );
>> 
>>  JSON_VALUE_STRING(, “value2");
>>  json_encode_object_entry(encoder, “name2", );
>> 
>>  json_encode_array_name(encoder, “name3");
>>  json_encode_array_start(encoder);
>> 
>>  if(getType){
>>  json_encode_object_start(module);
>>  
>>  JSON_VALUE_INT(, 4);
>>  json_encode_object_entry(module, “name4", );
>> 
>>  JSON_VALUE_INT(, 5);
>>  json_encode_object_entry(module, “name5", );
>>  
>>  json_encode_object_finish(module);
>>  }
>>  
>>  json_encode_array_finish(encoder);
>>  json_encode_object_finish(encoder);
>> 
>> 
>> 
>>> On 28-Feb-2018, at 1:18 PM, marko kiiskila  wrote:
>>> 
>>> Hi Aditya,
>>> 
 On Feb 28, 2018, at 9:26 AM, Aditya Xavier  wrote:
 
 HI Mynewt Team,
 
Wanted some assistance on how to encode and decode the following JSON 
 string.
 
{“name1":1,”name2”:"value2”,"name3":[{“name4":1,”name5":5}]}
 
Because of sparse documentation, I used the Test sample; however am 
 stuck with encoding an array of objects.
 
>>> 
>>> …
>>> 
 
I followed the example provided at 
 https://mynewt.apache.org/latest/os/modules/json/json_encode_object_entry 
 
 
And it gives me error :-
error: implicit declaration of function 'json_encode_object_start'
 
 Thanks,
 Aditya Xavier.
>>> 
>>> you need to #include  to see function/macro declarations.
>>> 
>> 
> 



Re: JSON Encoding and Decoding

2018-02-28 Thread marko kiiskila


> On Feb 28, 2018, at 10:10 AM, Aditya Xavier  wrote:
> 
> Yes, that was derp from my end.
> 
> Any clue on how to encode the JSON structure I mentioned ?
> 
> Is this method correct ?

You need to initialize the encoder; it needs to be told where to write
the encoded data. Specifically, you need to at least fill in je_write
function pointer. This is who’ll get the stream of encoded data.

Take a look at unit test @ encoding/json/test/src/testcases/json_simple_encode.c

Otherwise it looks pretty good, based on my cursory inspection.

> 
>   struct json_encoder *encoder, *module;
>   struct json_value data;
> 
>   memset(, 0, sizeof(encoder));
>   memset(, 0, sizeof(module));
> 
>   json_encode_object_start(encoder);
> 
>   JSON_VALUE_INT(, 1);
>   json_encode_object_entry(encoder, “name1", );
> 
>   JSON_VALUE_STRING(, “value2");
>   json_encode_object_entry(encoder, “name2", );
> 
>   json_encode_array_name(encoder, “name3");
>   json_encode_array_start(encoder);
> 
>   if(getType){
>   json_encode_object_start(module);
>   
>   JSON_VALUE_INT(, 4);
>   json_encode_object_entry(module, “name4", );
> 
>   JSON_VALUE_INT(, 5);
>   json_encode_object_entry(module, “name5", );
>   
>   json_encode_object_finish(module);
>   }
>   
>   json_encode_array_finish(encoder);
>   json_encode_object_finish(encoder);
> 
> 
> 
>> On 28-Feb-2018, at 1:18 PM, marko kiiskila  wrote:
>> 
>> Hi Aditya,
>> 
>>> On Feb 28, 2018, at 9:26 AM, Aditya Xavier  wrote:
>>> 
>>> HI Mynewt Team,
>>> 
>>> Wanted some assistance on how to encode and decode the following JSON 
>>> string.
>>> 
>>> {“name1":1,”name2”:"value2”,"name3":[{“name4":1,”name5":5}]}
>>> 
>>> Because of sparse documentation, I used the Test sample; however am 
>>> stuck with encoding an array of objects.
>>> 
>> 
>> …
>> 
>>> 
>>> I followed the example provided at 
>>> https://mynewt.apache.org/latest/os/modules/json/json_encode_object_entry 
>>> 
>>> 
>>> And it gives me error :-
>>> error: implicit declaration of function 'json_encode_object_start'
>>> 
>>> Thanks,
>>> Aditya Xavier.
>> 
>> you need to #include  to see function/macro declarations.
>> 
> 



Re: JSON Encoding and Decoding

2018-02-28 Thread Aditya Xavier
Yes, that was derp from my end.

Any clue on how to encode the JSON structure I mentioned ?

Is this method correct ?

struct json_encoder *encoder, *module;
struct json_value data;

memset(, 0, sizeof(encoder));
memset(, 0, sizeof(module));

json_encode_object_start(encoder);

JSON_VALUE_INT(, 1);
json_encode_object_entry(encoder, “name1", );

JSON_VALUE_STRING(, “value2");
json_encode_object_entry(encoder, “name2", );

json_encode_array_name(encoder, “name3");
json_encode_array_start(encoder);

if(getType){
json_encode_object_start(module);

JSON_VALUE_INT(, 4);
json_encode_object_entry(module, “name4", );

JSON_VALUE_INT(, 5);
json_encode_object_entry(module, “name5", );

json_encode_object_finish(module);
}

json_encode_array_finish(encoder);
json_encode_object_finish(encoder);



> On 28-Feb-2018, at 1:18 PM, marko kiiskila  wrote:
> 
> Hi Aditya,
> 
>> On Feb 28, 2018, at 9:26 AM, Aditya Xavier  wrote:
>> 
>> HI Mynewt Team,
>> 
>>  Wanted some assistance on how to encode and decode the following JSON 
>> string.
>> 
>>  {“name1":1,”name2”:"value2”,"name3":[{“name4":1,”name5":5}]}
>> 
>>  Because of sparse documentation, I used the Test sample; however am 
>> stuck with encoding an array of objects.
>> 
> 
> …
> 
>> 
>>  I followed the example provided at 
>> https://mynewt.apache.org/latest/os/modules/json/json_encode_object_entry 
>> 
>> 
>>  And it gives me error :-
>>  error: implicit declaration of function 'json_encode_object_start'
>> 
>> Thanks,
>> Aditya Xavier.
> 
> you need to #include  to see function/macro declarations.
> 



Re: JSON Encoding and Decoding

2018-02-27 Thread marko kiiskila
Hi Aditya,

> On Feb 28, 2018, at 9:26 AM, Aditya Xavier  wrote:
> 
> HI Mynewt Team,
> 
>   Wanted some assistance on how to encode and decode the following JSON 
> string.
> 
>   {“name1":1,”name2”:"value2”,"name3":[{“name4":1,”name5":5}]}
> 
>   Because of sparse documentation, I used the Test sample; however am 
> stuck with encoding an array of objects.
> 

…

> 
>   I followed the example provided at 
> https://mynewt.apache.org/latest/os/modules/json/json_encode_object_entry 
> 
> 
>   And it gives me error :-
>   error: implicit declaration of function 'json_encode_object_start'
> 
> Thanks,
> Aditya Xavier.

you need to #include  to see function/macro declarations.