You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mynewt.apache.org by Aditya Xavier <ad...@me.com> on 2018/03/01 07:36:14 UTC
Re: JSON Encoding and Decoding
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 = &visible
}
},
{NULL},
};
int fetch_map(const char *map){
// int i;
buf_init(&tjb,(char *) map);
console_printf("Buffer Initiated\n");
json_read_object(&tjb.json_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 :-
000001 Buffer Initiated
000002 JSON Read 0!
000003 PRN = 10, elevation = 45, azimuth = 196
000004 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 <ad...@me.com> 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 <ch...@runtime.io> 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 <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
>