interactive-matter / aJson

aJson is an Arduino library to enable JSON processing with Arduino. It easily enables you to decode, create, manipulate and encode JSON directly from and to data structures.
http://interactive-matter.org/2010/08/ajson-handle-json-with-arduino/
566 stars 136 forks source link

Can you create a very simple sketch that follows the filtering example described in the readme? #48

Open rkertesz opened 11 years ago

rkertesz commented 11 years ago

I will be the first to admit that I am not a strong programmer in C/C++. I could really use an example sketch that uses the "Filtering while parsing" method described in the README.md file. It would help me through the following problem

I was getting an error while compiling that the "scalar object requires one element in initializer". To my understanding, the use of the char* is a pointer to a pointer but I honestly don't know what is needed to initialize this correctly: char* jsonFilter = {"name,"format","height","width",NULL};

Also of note, there is a missing close quote after {"name in the README file.

rkertesz commented 11 years ago

I posted to pastebin here. http://pastebin.com/naF0C6sn

rkertesz commented 11 years ago

This is slightly different from the parsing example. The parsing example doesn't do any filtering from what I can tell. I want to use the filtering method to manipulate only the pieces of information I need from the tree rather than store the whole string response and then also store the whole tree before parsing. I thought that the filtering example in the read me was designed to reduce the memory load. I thought that the code for filtering, combined with the code for streaming would allow me to dump the information that I don't need rather than store it, saving me space.

sudar commented 11 years ago

@I8p Yeah, I realized it as soon as I replied. That's why I removed my comment.

Give me a couple of days, I will try it out and get back with a working example.

sjunnesson commented 11 years ago

does the above pastebin compile for you? Im getting the below error when I just do a straight copy paste from the bin

sketch_aug22a.ino: In function 'char* parseJson(char*)': sketch_aug22a:63: error: scalar object 'jsonFilter' requires one element in initializer

epiccoolguy commented 11 years ago

If I have the following response and store it for parsing:

{
    "sensor": {
        "id": "418143",
        "name": "analogpin0",
        "type": "0",
        "device_type": "Pressure Sensor",
        "data_type_id": "35501",
        "pager_type": "",
        "display_name": "Analog Pin A0",
        "use_data_storage": true,
        "data_type": "json",
        "data_structure": "{\"value\":\"Integer\"}"
    }
}

Shouldn't the following work?

char** jsonFilter = (char*[]) {"sensor","id","display_name",NULL};

Explanation I found here: http://compsci.ca/v3/viewtopic.php?p=223745#223745

aJson.h only has the following functions though:

parse(aJsonStream*)
parse(aJsonStream*, char**) // This one should be able to use a filter
parse(char*)

According to the header file you will need a stream in order to use a filter. I haven't gotten a http client working with streams (yet) so I'm not able to test this.

If this isn't correct, please let me know. I'm still learning about C and the Arduino ecosystem.

rkertesz commented 11 years ago

@sjunnesson that is correct. I was not able to compile it. I am looking for help in determining why it doesn't work. Novice here.

rkertesz commented 11 years ago

@epiccoolguy My JSON response is too large to store and then search. That is why I need to use streaming to look for the information as it is being streamed. When you say you haven't set up an http client to stream, do you mean that I something has to be done to a json stream before sending it to the arduino? Why use ajson if you use a computer in T&E first place?

epiccoolguy commented 11 years ago

@I8p Not a json stream, aJsonStream is a class defined in this library.

Have you looked at how the EthernetClient examples on the Arduino Playground work? I would recommend looking at how the (Arduino) Streams, the Ethernet library and this library work.

You need to feed parse(aJsonStream* stream, char** filter) an aJsonStream* together with a filter in order to get the data from the response without buffering the entire response. Looking at the header file, there's an object which inherits aJsonStream and accepts a Stream (such as Serial, EthernetClient) as parameter:

/* JSON stream that consumes data from a connection (usually
 * Ethernet client) until the connection is closed. */
class aJsonClientStream : public aJsonStream {
public:
    aJsonClientStream(Client *stream_)
        : aJsonStream(NULL), client_obj(stream_)
        {}

Try passing this constructor the client you're using to receive the data and then pass that aJsonClientStream to parse(aJsonStream* stream, char** filter).

I can't remember what I've tried myself. I gave up eventually on using additional libraries since they were also massive (example sketch size 90%) and instead used string.h functions to parse http responses.

This library would've worked nice in combination with interactive-matter/HTTPClient, but both are kind of abandoned and HTTPClient is still using C-style streams while aJson has been updated for use with Arduino style streams.

rkertesz commented 11 years ago

Thank you! ... It does seem like they are not in development. I am trying to see if I can parse the data using a Yun instead. More memory etc. I'll have to dig into Linux tools to do so I think.

I8P

On Oct 14, 2013, at 6:09 PM, Miguel Lo-A-Foe notifications@github.com wrote:

@I8p Not a json stream, aJsonStream is a class defined in this library.

Have you looked at how the EthernetClient examples on the Arduino Playground work? I would recommend looking at how the (Arduino) Streams, the Ethernet library and this library work.

You need to feed parse(aJsonStream* stream, char* filter) an aJsonStream\ together with a filter in order to get the data from the response without buffering the entire response. Looking at the header file, there's an object which inherits aJsonStream and accepts a Stream (such as Serial, EthernetClient) as parameter:

/* JSON stream that consumes data from a connection (usually

  • Ethernet client) until the connection is closed. / class aJsonClientStream : public aJsonStream { public: aJsonClientStream(Client *stream) : aJsonStream(NULL), clientobj(stream) {} Try passing this constructor the client you're using to receive the data and then pass that aJsonClientStream to parse(aJsonStream_ stream, char\ filter).

I can't remember what I've tried myself. I gave up eventually on using additional libraries since they were also massive (example sketch size 90%) and instead used string.h functions to parse http responses.

This library would've worked nice in combination with interactive-matter/HTTPClient, but both are kind of abandoned and HTTPClient is still using C-style streams while aJson has been updated for use with Arduino style streams.

— Reply to this email directly or view it on GitHub.

lyssbach commented 10 years ago

I am having exactly the same issue when initating the filter string for my program to process the JSON feed from Yahoo.

initialize the filter: char* jsonFilter = (char []) {"query","results","channel","item","condition","text",NULL};

error from Arduino IDE (both 1.0.5 and 1.5.5) GetYahooWeather:222: error: no matching function for call to 'aJsonClass::parse(char_&, char&)' C:\Users\why\Documents\Arduino\libraries\aJson/aJSON.h:179: note: candidates are: aJsonObject aJsonClass::parse(aJsonStream) C:\Users\why\Documents\Arduino\libraries\aJson/aJSON.h:180: note: aJsonObject aJsonClass::parse(aJsonStream, char) C:\Users\why\Documents\Arduino\libraries\aJson/aJSON.h:181: note: aJsonObject_ aJsonClass::parse(char*)

Really like to have an example to understand the filter usage. Thanks.

atoudam commented 10 years ago

@I8p did you end getting the filtering / stream running? as im working on something similar at the moment

djMax commented 10 years ago

I don't see any evidence that the filter is used anywhere in the json parser code.