Example #1
0
static int erl_json_ei_string(void* ctx, const unsigned char* val, unsigned int len) {
  State* pState = (State*) ctx;

  flog(stderr, "string", 0, (const char*)val, len);
  
  list_header_for_value(pState);
  write_binary(pState, (const char*)val, len);
  return 1;
}
Example #2
0
static int erl_json_ei_null(void* ctx) {
  State* pState = (State*) ctx;
  
  flog(stderr, "null", 0, 0, 0);
  
  list_header_for_value(pState);
  write_atom(pState, "null", 4);

  return 1;
}
Example #3
0
static int erl_json_ei_null(void* ctx) {
  State* pState = (State*) ctx;
  
  flog(stderr, "null", 0, 0, 0);
  
  list_header_for_value(pState);
  ei_x_encode_atom_len(&pState->ei_buf, "null", 4);

  return 1;
}
Example #4
0
static int erl_json_ei_start_array(void* ctx) {
  State* pState = (State*) ctx;

  flog(stderr, "start array", 0, 0, 0);
  
  list_header_for_value(pState);
  
  pState->skip_list_header_for_value = pState->ei_buf.index;
  ei_x_encode_list_header(&pState->ei_buf, 1);

  return 1;
}
Example #5
0
static int erl_json_ei_number(void* ctx, const char * val, unsigned int len) {
  State* pState = (State*) ctx;

  flog(stderr, "number", 0, val, len);

  list_header_for_value(pState);

  switch(numbers_as(pState)) {
    case EEP0018_PARSE_NUMBERS_AS_NUMBER:
    {
      if(memchr(val, '.', len) || memchr(val, 'e', len) || memchr(val, 'E', len))
        write_double(pState, strtod(val, 0));
      else
        write_long(pState, strtol(val, 0, 10));
      break;
    }
    case EEP0018_PARSE_NUMBERS_AS_FLOAT:
    {
      write_double(pState, strtod(val, 0));
      break;
    }
    case EEP0018_PARSE_NUMBERS_AS_TUPLE:
    {
      /* 
        While "1e1" is a valid JSON number, it is not a valid parameter to list_to_float/1 
        We fix that by inserting ".0" before the exponent e. 
      */
      const char* exp = memchr(val, 'e', len);
      if(exp && exp > val) {
        const char* dot = memchr(val, '.', exp - val);
        if(!dot) {
          char* tmp = alloca(len + 5);
          memcpy(tmp, val, exp - val);
          memcpy(tmp + (exp - val), ".0", 2);
          memcpy(tmp + (exp - val) + 2, exp, len - (exp - val));
          len += 2;
          val = tmp;
          tmp[len] = 0;
        }
      }

      write_tuple_header(pState, 3);
      write_atom(pState, "number", 6);
      write_string(pState, val, len);
      write_atom(pState, "a", 1);       // a dummy
      break;
    }
  }

  
  return 1;
}
Example #6
0
static int erl_json_ei_boolean(void* ctx, int boolVal) {
  State* pState = (State*) ctx;

  flog(stderr, boolVal ? "true" : "false", 0, 0, 0);
  
  list_header_for_value(pState);
  
  if(boolVal)
    write_atom(pState, "true", 4);
  else
    write_atom(pState, "false", 5);

  return 1;
}
Example #7
0
static int erl_json_ei_boolean(void* ctx, int boolVal) {
  State* pState = (State*) ctx;

  flog(stderr, boolVal ? "true" : "false", 0, 0, 0);
  
  list_header_for_value(pState);
  
  if(boolVal)
    ei_x_encode_atom_len(&pState->ei_buf, "true", 4);
  else
    ei_x_encode_atom_len(&pState->ei_buf, "false", 5);

  return 1;
}
Example #8
0
static int erl_json_ei_map_key(void* ctx, const unsigned char* buf, unsigned int len) {
  State* pState = (State*) ctx;

  flog(stderr, "map key", 0, buf, len);

  list_header_for_value(pState);
  
  write_tuple_header(pState, 2);

  switch(keys_as(pState)) {
    case EEP0018_PARSE_KEYS_AS_ATOM:
      write_atom(pState, (const char*)buf, len);
      break;
    case EEP0018_PARSE_KEYS_AS_BINARY:
      write_binary(pState, (const char*)buf, len);
      break;
  }

  pState->skip_list_header_for_value = -1;
  
  return 1;
}