The zero-based JSON library

The zero-based JSON library

Project Address jsoncpp , this project has been leptjson inspired to achieve the most basic function, only for learning to use.

  • Provides a simple parse () and generate () method JSON text parsed into objects, and vice versa
  • Only supports the primitive type Value of JSON and JSON text abstract syntax tree conversion
  • Using standard C / C ++ language (C ++ 11)
  • We do not rely on third-party libraries

start up

Start this project needs to be installed gcc4.8 or later (to support c ++ 11)

rm *.o
rm start
g++ -w -c -std=c++11 jsonValue.cpp
g++ -w -c -std=c++11 jsonGenerator.cpp
g++ -w -c -std=c++11 jsonParser.cpp
g++ -w -c -std=c++11 test.cpp
g++ -w jsonValue.o jsonGenerator.o jsonParser.o test.o -o start
./start

Preliminary entry

Then we can start thinking about a simpler problems start with how to resolve some json text.

1, given the long text "[1,2,3]", asking how to get an array?

When we want to resolve a continuous function is called to deal with a long text, analytic functions required realized in two steps:

  • step1 parse text and update the text pointer.
  • step2 successfully resolved if the analysis result is returned.

Here analytic functions only need to call three times, the last encounter ']' to end

class Parser{
    char *txt;
    int parse_value() {
        /* 解析txt上下文 */
        /* 返回一个数值 */
    }
}

int main() {
    /* 建立parser */
    while (*parser.txt != ']') {
        array[i] = parser.parse_value();
    }
}
2, given the long text "[1, 2, [4,5]]", asking how to get a tree?

Similarly, there is need to call analytic functions three times, the last encounter ']' to end

typedef struct {
    int number;
    vector<Node* > array;
} TreeNode

class Parser{
    char* txt;
    TreeNode* parse_value() {
        /* 解析txt上下文 */
        /* 返回一个节点 */
    }
}

int main() {
    /* 建立parser */
    /* 建立树根 */
    while (*parser.txt != ']') {
        root.array[i] = parser.parse_value();
    }
}

After the completion of the analytical long text, and then consider the analytic function itself to achieve. '1', '2' can be resolved directly, while "[4,5]" can be seen as a long text, to make recursive calls.

The above process I have adopted the object-oriented design, each operation is a function of the object. Of course, we can also adopt process-oriented thinking, but be sure to address incoming txt text pointer for each function, or the use of reference.

3, given the long text JSON, asking how to get a tree?

The project jsoncpp is the answer to this question.

Guess you like

Origin www.cnblogs.com/zzzz76/p/12629456.html