Bug in IDE : using json in string

Hi all,

I don't know if it's a bug, but I'm experiencing some strange behaviours using the web IDE ...

when I try to do the following :

void loop()
{
   
    char registerObject[80] = "{name:'testdevice',type:'test',tags:['a','b'],description:'testdesc',id:'1000'}";
    char registerTargetPost[62] = "POST http://172.16.0.33:8000/api/v1/devices/register HTTP/1.1";
    Serial.println(registerObject);
    Serial.println(registerTargetPost);
    delay(SLEEP_TIME);        
}    

I get the following compile error :

../2009d6b6884651d5335ba2fda44d4161dbce3e429974a3933b25a619c3fb/the_user_app.cpp:262:35: error: missing terminating " character [-Werror]
../2009d6b6884651d5335ba2fda44d4161dbce3e429974a3933b25a619c3fb/the_user_app.cpp:262:5: error: missing terminating " character
../2009d6b6884651d5335ba2fda44d4161dbce3e429974a3933b25a619c3fb/the_user_app.cpp: In function 'void loop()':
../2009d6b6884651d5335ba2fda44d4161dbce3e429974a3933b25a619c3fb/the_user_app.cpp:263:34: error: array must be initialized with a brace-enclosed initializer
cc1plus: all warnings being treated as errors
make: *** [../2009d6b6884651d5335ba2fda44d4161dbce3e429974a3933b25a619c3fb/the_user_app.o] Error 1

I don't know if somebody else had the same problems ? Or if I'm missing something here ...

But to have some more info about what I'm trying to do is : I'm trying to post data to a server to track it. I know there is SSE ... but that isn't functional yet?

But if someone has any idea ...

Thanks !!

Are the strings getting split across lines ?

If the IDE forces the split, try putting \ at the end of the first line of each string, to escape the newline.

for instance:

  char registerObject[80] = "{name:'testdevice',type:'test',tags:\

[‘a’,‘b’],description:‘testdesc’,id:‘1000’}";

Hi ,

no they’re not getting splitted … it’s just this forum that splits it :smile:

so the’re basically on one line… I think somehow, in the pre-processor or so, this stirng gets parsed and sees a ‘{’ … somehow it interprets this as the beginning of an '{}.

But that’s just what I’m suspecting … don’t know if this is the problem :wink:. I’m hoping that there is someone that can confirm this …

Then I’m out of ideas, that’s valid vanilla C - no idea what manglage might occur behind the scenes before that gets presented to a compiler.

I know ...

I was trying to use it to perform a HTTP Post message... but when i just use the following code :

client.println("POST http://172.16.0.33:8000/api/v1/devices/register HTTP/1.1");
client.println("User-Agent: Fiddler");
client.println("Host: 172.16.0.33:8000");
client.println("Content-Length: 79");
client.println();
client.println("{name:'testdevice',type:'test',tags:['a','b'],description:'testdesc',id:'1000'}");
client.flush();

I get a post message on the server side ... but the content of the body is always empty ... that's why I was trying to use a variable to send the message ... and so I noticed this error ...

But ... maybe this is the same problem? Getting an empty body at my server ... ? Gonna try this without the '{}' and add them server-side ...

Another strange thing :

client.println("POST http://172.16.0.33:8000/api/v1/devices/register HTTP/1.1");

results in a compile error … while the following doesn’t :

client.println("POST /api/v1/devices/register HTTP/1.1");

Don’t know what I’m missing there …

I think you are having problems due to the curly braces--something in the IDE (preprocessor?) is giving them higher priority. I have had problems in commented out code with curly braces!

I have a serial LCD display on one of my cores and doing this:

Serial1.print("{http://}"); //does not compile

but this does compile:

const char tryit = { '{', 'h','t','t','p',':','/','/','}','\0'};
Serial1.print(tryit);

I am sure you just need to split out the curly braces and not the entire string.

Hope this helps!

Hi Guys,

Thank you for catching this, you're absolutely right this is a compiler pre-processor bug. It looks like the compiler thinks the "//" is a comment, and it strips it out of the program:

this:

void loop() {
    Serial.println("POST http://172.16.0.33:8000/api/v1/devices/register HTTP/1.1");
}

becomes:

void loop() {
    Serial.println("POST http:
}

I'm adding this to our bug list for the compiler, and hopefully I'll get a chance to patch it this weekend. Sorry about that. If you split your slashes then it should ignore it as a workaround.

Thanks,
David

1 Like

Thanks Dave! Good to know it is the slashes (not the curly braces as I thought). Easy to work around when you know what it is!

1 Like

Thanks for the help!!

1 Like

hey,
i have quite the same problem. i try #define SONOS_PAUSE "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:Pause xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:Pause></s:Body></s:Envelope>"
without succes.
Is there any possibility to compile this part?

thanks
Philipp

@Dave is working on the fix for this as we speak! Should be available by the end of the week.

2 Likes

I have a fix for this in queue to be deployed, hoping to test this and roll it out to production tomorrow. :slight_smile: In the meantime, I suspect the issue is just the “//” in the string, the pre-processor incorrectly thinks that it is a comment. If you broke that into two parts it’d work in the meantime I’m guessing.

Are you interfacing with a Sonos? I’d be really curious to see what you make, I was thinking a Sonos remote control of some kind would be really slick… :smiley:

Thanks,
David

1 Like

Just wanted to followup here that this bug is fixed and has been rolled out. Please use things that look like comments “//” “/*” in strings with impunity!

Thanks,
David

@Dave do you just replace all ‘/’ with character escaping like ‘/’ ? What if we use character escaping in our strings by default? Before you updated the compiler I was character escaping everything with success.

The pre-processor just strips out anything it thinks is a code comment, if you send me an example of code that isn’t working I’ll add a unit test for it :smile:

Thanks,
David

I was doing some similar things with parsing JSON, and I had to escape most of the special characters. See here. This currently builds and works, but does not if you use // or \ or { or } or " in the string without escaping.

#define UPDATE_INTERVAL 1000
bool state = false;
char output[20];
uint32_t lastTime = 0;
char response[1024] ="\{\"about\":\"http:\/\/www.zara.com\",\"category\":\"Clothing\",\"description\":\"Welcome to ZARA\u2019s official Facebook page.\n\nHelp us keep this page clean by avoiding any indecent or harmful material including but not limited to:profanity,obscenity,illegal,off-topic or promotional content.\n\nZARA may remove User Generated Content that exceeds these guidelines and reserves the right to ban those users who intentionally and repeatedly ignore them.\",\"founded\":\"1975\",\"is_published\":true,\"products\": \"WOMAN \u00b7 TRF \u00b7 MAN \u00b7 KIDS\",\"talking_about_count\": 68161,\"username\": \"Zara\",\"website\": \"http:\/\/www.zara.com\",\"were_here_count\": 0,\"id\": \"33331950906\",\"name\": \"ZARA\",\"link\": \"http:\/\/www.facebook.com\/Zara\",\"likes\": 20129918,\"cover\": \{\"cover_id\": \"10151778060075907\",\"source\":\"http:\/\/scontent-a.xx.fbcdn.net\/hphotos-ash3\/t1\/s720x720\/1005203_10151778060075907_251016347_n.jpg\",\"offset_y\":0,\"offset_x\":0\}\}";
char *s;
char result[100];
uint32_t likes_val = 0;
char * pEnd;

void setup() {
  pinMode(D7,OUTPUT);
  digitalWrite(D7,HIGH);
  Serial.begin(9600);
  while(!Serial.available()); // you have to open your Serial monitor and press ENTER
}

void loop() {
  if(millis() - lastTime > UPDATE_INTERVAL) {
    lastTime = millis();
    
    s = strstr(response, "\"likes\": ");    // search for string ["likes": ] in response
    if (s != NULL)                          // if succesfull then s now points at ["likes": ]
    {
        s += 9; //advance pointer to likes number.
        int x = 0;
        while(x<20 && *s != ',') {
            result[x++] = *s++;
        }
        result[x] = '\0';
        likes_val = strtol(result,&pEnd,10); // convert string to long int if you need a working number
        Serial.print("Found likes: ");
        Serial.println(likes_val);
    }
    else
    {
        Serial.println("Likes not found!\n");  // `strstr` returns NULL if search string not found
    }
    digitalWrite(D7,state);
    state = !state;
  }
}

This answers my question, you are escaping by default now:

Ok, there may still be some issues with escaping strings for us… like how do we control the usage of single and double quotes… for example, can you make this compile?

http://graph.facebook.com/sparkdevices

char response[1024] = "{"about": "Spark makes using and creating connected devices easy. Learn more and pre-order the Spark Core and accessories at www.spark.io", "category": "Computers/technology", "description": "Spark makes using and creating connected devices easy. The Spark Core an Arduino-compatible, Wi-Fi enabled development kit pairs with the Spark Cloud powered development platform to make creating internet-connected hardware a breeze now available for pre-order at: https://www.spark.io", "is_published": true, "location": {"street": "", "city": "Minneapolis", "state": "MN", "country": "United States", "zip": ""}, "products": "Spark Core, Spark Cloud", "talking_about_count": 395, "username": "sparkdevices", "website": "http://www.spark.io", "were_here_count": 0, "id": "427120970660412", "name": "Spark", "link": "http://www.facebook.com/sparkdevices", "likes": 2363, "cover": {"cover_id": 537602636278911, "source": "http://sphotos-f.ak.fbcdn.net/hphotos-ak-prn1/s720x720/923349_537602636278911_656089984_n.jpg", "offset_y": 40, "offset_x": 0 } }";

I tried for a while escaping things without winning… :smile:

Well that’s not good! Working on a fix now. – Sorry about the delay, I’ve been kinda sick for a few days now

Edit: The compiler wasn’t escaping, it had been trying to filter out comments, but I think this approach just ends up being problematic. I’m changing the approach slightly and I think that’ll be better. I’m going to test and try to roll out improvements to this today. I think the problem was that the regex saw the escaped quotes inside the string, and decided the comment wasn’t in a string, etc.

Thanks!
David

1 Like