zserge / jsmn

Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket
MIT License
3.65k stars 778 forks source link

Unexpected leniency with JSMN_STRICT defined #178

Open mulle-nat opened 4 years ago

mulle-nat commented 4 years ago

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);
}
pt300 commented 4 years ago

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