src/c/json.c
static int iot_json_parse_string (iot_json_parser *parser, const char *js, size_t len, iot_json_tok_t *tokens, uint32_t num_tokens)
{
  bool escaped = false;
  iot_json_tok_t *token;
  uint32_t start = parser->pos;
  parser->pos++;

  /* Skip starting quote */
  for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++)
  {
    char c = js[parser->pos];

    /* Quote: end of string */
    if (c == '\"')
    {
      if (tokens == NULL)
      {
        return 0;
      }
      token = iot_json_alloc_token (parser, tokens, num_tokens);
      if (token == NULL)
      {
        parser->pos = start;
        return IOT_JSON_ERROR_NOMEM;
      }
      iot_json_fill_token (token, escaped ? IOT_JSON_STRING_ESC : IOT_JSON_STRING, start + 1, parser->pos);
      token->parent = parser->toksuper;
      return 0;
    }

    /* Backslash: Quoted symbol expected */
    if (c == '\\' && parser->pos + 1 < len)
    {
      parser->pos++;
      escaped = true;
      switch (js[parser->pos])
      {
        /* Allowed escaped symbols */
        case '\"':
        case '/':
        case '\\':
        case 'b':
        case 'f':
        case 'r':
        case 'n' :
        case 't':
          break;
          /* Allows escaped symbol \uXXXX */
        case 'u':
          parser->pos++;
          for (int i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++)
          {
            /* If it isn't a hex character we have an error */
            if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
                  (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
                  (js[parser->pos] >= 97 && js[parser->pos] <= 102)))
            { /* a-f */
              parser->pos = start;
              return IOT_JSON_ERROR_INVAL;
            }
            parser->pos++;
          }
          parser->pos--;
          break;
          /* Unexpected symbol */
        default:
          parser->pos = start;
          return IOT_JSON_ERROR_INVAL;
      }
    }
  }
  parser->pos = start;
  return IOT_JSON_ERROR_PART;
}
/**
 * Fills next token with JSON string.
 */
Fills next token with JSON string.
Fills next token with JSON string .
static int iot_json_parse_string (iot_json_parser *parser, const char *js, size_t len, iot_json_tok_t *tokens, uint32_t num_tokens)
{
  bool escaped = false;
  iot_json_tok_t *token;
  uint32_t start = parser->pos;
  parser->pos++;
  for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++)
  {
    char c = js[parser->pos];
    if (c == '\"')
    {
      if (tokens == NULL)
      {
        return 0;
      }
      token = iot_json_alloc_token (parser, tokens, num_tokens);
      if (token == NULL)
      {
        parser->pos = start;
        return IOT_JSON_ERROR_NOMEM;
      }
      iot_json_fill_token (token, escaped ? IOT_JSON_STRING_ESC : IOT_JSON_STRING, start + 1, parser->pos);
      token->parent = parser->toksuper;
      return 0;
    }
    if (c == '\\' && parser->pos + 1 < len)
    {
      parser->pos++;
      escaped = true;
      switch (js[parser->pos])
      {
        case '\"':
        case '/':
        case '\\':
        case 'b':
        case 'f':
        case 'r':
        case 'n' :
        case 't':
          break;
        case 'u':
          parser->pos++;
          for (int i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++)
          {
            if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || 
                  (js[parser->pos] >= 65 && js[parser->pos] <= 70) || 
                  (js[parser->pos] >= 97 && js[parser->pos] <= 102)))
            { 
              parser->pos = start;
              return IOT_JSON_ERROR_INVAL;
            }
            parser->pos++;
          }
          parser->pos--;
          break;
        default:
          parser->pos = start;
          return IOT_JSON_ERROR_INVAL;
      }
    }
  }
  parser->pos = start;
  return IOT_JSON_ERROR_PART;
}
static int iot_json_parse_string ( iot_json_parser * parser , const char * js , size_t len , iot_json_tok_t * tokens , uint32_t num_tokens ) { bool escaped = false ; iot_json_tok_t * token ; uint32_t start = parser -> pos ; parser -> pos ++ ; for ( ; parser -> pos < len && js [ parser -> pos ] != ' \0 ' ; parser -> pos ++ ) { char c = js [ parser -> pos ] ; if ( c == ' \" ' ) { if ( tokens == NULL ) { return 0 ; } token = iot_json_alloc_token ( parser , tokens , num_tokens ) ; if ( token == NULL ) { parser -> pos = start ; return IOT_JSON_ERROR_NOMEM ; } iot_json_fill_token ( token , escaped ? IOT_JSON_STRING_ESC : IOT_JSON_STRING , start + 1 , parser -> pos ) ; token -> parent = parser -> toksuper ; return 0 ; } if ( c == ' \\ ' && parser -> pos + 1 < len ) { parser -> pos ++ ; escaped = true ; switch ( js [ parser -> pos ] ) { case ' \" ' : case ' ' : case ' \\ ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : break ; case ' ' : parser -> pos ++ ; for ( int i = 0 ; i < 4 && parser -> pos < len && js [ parser -> pos ] != ' \0 ' ; i ++ ) { if ( ! ( ( js [ parser -> pos ] >= 48 && js [ parser -> pos ] <= 57 ) || ( js [ parser -> pos ] >= 65 && js [ parser -> pos ] <= 70 ) || ( js [ parser -> pos ] >= 97 && js [ parser -> pos ] <= 102 ) ) ) { parser -> pos = start ; return IOT_JSON_ERROR_INVAL ; } parser -> pos ++ ; } parser -> pos -- ; break ; default : parser -> pos = start ; return IOT_JSON_ERROR_INVAL ; } } } parser -> pos = start ; return IOT_JSON_ERROR_PART ; }
Fills next token with JSON string.
Fills next token with JSON string .
/* Skip starting quote */ /* Quote: end of string */ /* Backslash: Quoted symbol expected */ /* Allowed escaped symbols */ /* Allows escaped symbol \uXXXX */ /* If it isn't a hex character we have an error */ /* 0-9 */ /* A-F */ /* a-f */ /* Unexpected symbol */