Exemple #1
0
void text_print_multiline_formatted(nvObj_t *nv)
{
	for (uint8_t i=0; i<NV_BODY_LEN-1; i++) {
		if (nv->valuetype != TYPE_PARENT) {
			preprocess_float(nv);
			nv_print(nv);
		}
		if ((nv = nv->nx) == NULL) return;
		if (nv->valuetype == TYPE_EMPTY) break;
	}
}
Exemple #2
0
void text_print_inline_values(nvObj_t *nv)
{
	uint32_t *v = (uint32_t*)&nv->value;
	for (uint8_t i=0; i<NV_BODY_LEN-1; i++) {
		switch (nv->valuetype) {
			case TYPE_PARENT: 	{ if ((nv = nv->nx) == NULL) return; continue;} // NULL means parent with no child
			case TYPE_FLOAT:	{ preprocess_float(nv);
								  fntoa(global_string_buf, nv->value, nv->precision);
								  fprintf_P(stderr,PSTR("%s"), global_string_buf) ; break;
								}
			case TYPE_INTEGER:	{ fprintf_P(stderr,PSTR("%1.0f"), nv->value); break;}
			case TYPE_DATA:	    { fprintf_P(stderr,PSTR("%lu"), *v); break;}
			case TYPE_STRING:	{ fprintf_P(stderr,PSTR("%s"), *nv->stringp); break;}
			case TYPE_EMPTY:	{ fprintf_P(stderr,PSTR("\n")); return; }
		}
		if ((nv = nv->nx) == NULL) return;
		if (nv->valuetype != TYPE_EMPTY) { fprintf_P(stderr,PSTR(","));}
	}
}
Exemple #3
0
uint16_t json_serialize(nvObj_t *nv, char *out_buf, uint16_t size)
{
#ifdef __SILENCE_JSON_RESPONSES
	return (0);
#else
	char *str = out_buf;
	char *str_max = out_buf + size - BUFFER_MARGIN;
	int8_t initial_depth = nv->depth;
	int8_t prev_depth = 0;
	uint8_t need_a_comma = false;

	*str++ = '{'; 								// write opening curly

	while (true) {
		if (nv->valuetype != TYPE_EMPTY) {
			if (need_a_comma) { *str++ = ',';}
			need_a_comma = true;
			if (js.json_syntax == JSON_SYNTAX_RELAXED) {    // write name
				str += sprintf((char *)str, "%s:", nv->token);
			} else {
				str += sprintf((char *)str, "\"%s\":", nv->token);
			}

			// check for illegal float values
			if (nv->valuetype == TYPE_FLOAT) {
				if (isnan((double)nv->value) || isinf((double)nv->value)) { nv->value = 0;}
			}

			// serialize output value (arranged in rough order of likely occurrence)
			if      (nv->valuetype == TYPE_FLOAT)   { preprocess_float(nv);
			                                          str += fntoa(str, nv->value, nv->precision);}
			else if (nv->valuetype == TYPE_INT)     { str += sprintf((char *)str, "%1.0f", (double)nv->value);}
			else if (nv->valuetype == TYPE_STRING)  { str += sprintf((char *)str, "\"%s\"",(char *)*nv->stringp);}
			else if (nv->valuetype == TYPE_ARRAY)   { str += sprintf((char *)str, "[%s]",  (char *)*nv->stringp);}
			else if (nv->valuetype == TYPE_NULL)    { str += sprintf((char *)str, "null");} // Note that that "" is NOT null.
            else if (nv->valuetype == TYPE_DATA)    {
				uint32_t *v = (uint32_t*)&nv->value;
				str += sprintf(str, "\"0x%lx\"", *v);
            }
			else if (nv->valuetype == TYPE_BOOL) {
				if (fp_FALSE(nv->value)) {
                    str += sprintf(str, "false");
                } else {
                    str += sprintf(str, "true");
                }
			}
			else if (nv->valuetype == TYPE_PARENT) {
				*str++ = '{';
				need_a_comma = false;
			}
		}
		if (str >= str_max) { return (-1);}		// signal buffer overrun
		if ((nv = nv->nx) == NULL) { break;}	// end of the list

		while (nv->depth < prev_depth--) {		// iterate the closing curlies
			need_a_comma = true;
			*str++ = '}';
		}
		prev_depth = nv->depth;
	}

	// closing curlies and NEWLINE
	while (prev_depth-- > initial_depth) { *str++ = '}';}
	str += sprintf((char *)str, "}\n");	// using sprintf for this last one ensures a NUL termination
	if (str > out_buf + size) { return (-1);}
	return (str - out_buf);
#endif
}