jsmn icon indicating copy to clipboard operation
jsmn copied to clipboard

Unexpected leniency with JSMN_STRICT defined

Open mulle-nat opened this issue 4 years ago • 1 comments

If I parse a JSON [ thanx ] with JSMN_STRICT defined, it doesn't really complain. Though thanx is not a keyword and needs to be quoted:

#define JSMN_STRICT
#include "jsmn.h"
#include <stdio.h>
#include <string.h>


static char  *json = "[\n"
"   thanx\n"
"]";


int main( void)
{
   jsmn_parser   p;
   jsmntok_t     t[128];
   int           r;
   int           i;

   jsmn_init( &p);
   r = jsmn_parse(&p, json, strlen( json), t, 128);
   if( r < 0)
   {
      fprintf( stderr, "Failed to parse JSON: %d\n", r);
      return 1;
   }
   for( i = 0; i < r; i++)
      printf( "%.*s (%d)\n", t[ i].end - t[ i].start, &json[ t[ i].start], t[i].type);
   return( 0);
}

mulle-nat avatar Dec 18 '19 21:12 mulle-nat

That's due to how primitives are currently handled by JSMN. Only the first character is checked and if it matches t, f or n it is assumed those are respectively true, false or null

pt300 avatar Dec 18 '19 22:12 pt300