[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/zhangyuke-coder/json-tutorial/master/tutorial07/tutorial07.md [Back]  [Original]

#  JSON 

* Milo Yip
* 2016/12/20

[ JSON ](https://zhuanlan.zhihu.com/json-tutorial) [json-tutorial/tutorial07](https://github.com/miloyip/json-tutorial/blob/master/tutorial07)



1. [JSON ](#1-json-)
2. [ lept_context ](#2--lept_context-)
3. [ nullfalse  true](#3--nullfalse--true)
4. [](#4-)
5. [](#5-)

## 1. JSON 

 6  JSON  JSON  `lept_value` 

JSON generator JSON stringify

![JSON ](images/parse_stringify.png)

 API  JSON 

~~~c
char* lept_stringify(const lept_value* v, size_t* length);
~~~

`length`  JSON  `NULL`  `free()` 

prettify JSON 

## 2.  lept_context 

 JSON 

~~~c
#ifndef LEPT_PARSE_STRINGIFY_INIT_SIZE
#define LEPT_PARSE_STRINGIFY_INIT_SIZE 256
#endif

int lept_stringify(const lept_value* v, char** json, size_t* length) {
    lept_context c;
    int ret;
    assert(v != NULL);
    assert(json != NULL);
    c.stack = (char*)malloc(c.size = LEPT_PARSE_STRINGIFY_INIT_SIZE);
    c.top = 0;
    if ((ret = lept_stringify_value(&c, v)) != LEPT_STRINGIFY_OK) {
        free(c.stack);
        *json = NULL;
        return ret;
    }
    if (length)
        *length = c.top;
    PUTC(&c, '\0');
    *json = c.stack;
    return LEPT_STRINGIFY_OK;
}
~~~



 API  `length`  JSON  `strlen()`  JSON  JSON  `\u0000` `strlen()` 

## 3.  nullfalse  true

 JSON  3  JSON  TDD

~~~c
#define TEST_ROUNDTRIP(json)\
    do {\
        lept_value v;\
        char* json2;\
        size_t length;\
        lept_init(&v);\
        EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, json));\
        EXPECT_EQ_INT(LEPT_STRINGIFY_OK, lept_stringify(&v, &json2, &length));\
        EXPECT_EQ_STRING(json, json2, length);\
        lept_free(&v);\
        free(json2);\
    } while(0)

static void test_stringify() {
    TEST_ROUNDTRIP("null");
    TEST_ROUNDTRIP("false");
    TEST_ROUNDTRIP("true");
    /* ... */
}
~~~

 JSON  JSON JSON roundtrip JSON  `1.0`  `1` `lept_value` 

 `lept_stringify_value` `PUTS()` 

~~~c
#define PUTS(c, s, len)     memcpy(lept_context_push(c, len), s, len)

static int lept_stringify_value(lept_context* c, const lept_value* v) {
    size_t i;
    int ret;
    switch (v->type) {
        case LEPT_NULL:   PUTS(c, "null",  4); break;
        case LEPT_FALSE:  PUTS(c, "false", 5); break;
        case LEPT_TRUE:   PUTS(c, "true",  4); break;
        /* ... */
    }
    return LEPT_STRINGIFY_OK;
}
~~~

## 4. 

 `sprintf("%.17g", ...)` `"%.17g"` 



~~~c
        case LEPT_NUMBER:
            {
                char buffer[32];
                int length = sprintf(buffer, "%.17g", v->u.n);
                PUTS(c, buffer, length);
            }
            break;
~~~

 `PUTS()`  `memcpy()` `c`  `c->top`

~~~c
        case LEPT_NUMBER:
            {
                char* buffer = lept_context_push(c, 32);
                int length = sprintf(buffer, "%.17g", v->u.n);
                c->top -= 32 - length;
            }
            break;
~~~



~~~c
        case LEPT_NUMBER:
            c->top -= 32 - sprintf(lept_context_push(c, 32), "%.17g", v->u.n);
            break;
~~~

## 5. 

 JSON  leptjson 

leptjson  `lept_context` 

 RapidJSON  3  JSON 

1. JSON  `lept_stringify_string()` `0x20`  `\u00xx` 

2.  `lept_stringify_value()`  `switch`  JSON  `lept_stringify_value()`

3.  `lept_stringify_string()`  `PUTC()`

 [issue](https://github.com/miloyip/json-tutorial/issues) 

Web Proxy Viewer  |  New URL  |  Original Page