Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way of format just making a pretty print without changing the key's orders ? #713

Closed
zhishupp opened this issue Aug 26, 2017 · 2 comments

Comments

@zhishupp
Copy link

zhishupp commented Aug 26, 2017

for example, format like this, just adjust the indent, not change key's orders

{
	"arp table":	[{
			"Bucket":	88,
			"IPv4":	"1.0.0.1",
			"MAC":	"83:84:D4:D8:C6:AA",
			"Expire":	"static"
		}, {
			"Bucket":	998,
			"IPv4":	"1.0.0.2",
			"MAC":	"83:84:D4:D8:C6:BB",
			"Expire":	"static"
		}]
}
{
    "arp table": [
        {
            "Bucket": 88,
            "IPv4": "1.0.0.1",
            "MAC": "83:84:D4:D8:C6:AA",
            "Expire": "static"
        },
        {
            "Bucket": 998,
            "IPv4": "1.0.0.2",
            "MAC": "83:84:D4:D8:C6:BB",
            "Expire": "static"
        }
    ]
}
@nlohmann
Copy link
Owner

No, this is not possible at the moment.

See #660 #106 #424

@zhishupp
Copy link
Author

zhishupp commented Aug 28, 2017

void prettyJson(std::string& pretty_json, const std::string& json_string, std::string::size_type& pos, size_t le, size_t indent = JSON_INDENT)
{
    bool in_string = false;
    for (; pos < json_string.length(); ++pos)
    {
        char curr_char = json_string[pos];
        if ((curr_char == ' ' || curr_char == '\t') && !in_string)
        {
            continue;
        }
        else if ((curr_char == '\r' || curr_char == '\n') && !in_string)
        {
            continue;
        }
        else if ((curr_char == '{' || curr_char == '[') && !in_string)
        {
            pretty_json.append(1, curr_char);
            pretty_json.append(1, '\n');
            pretty_json.append((le + 1) * indent, ' ');
            prettyJson(pretty_json, json_string, ++pos, le + 1, indent);
        }
        else if ((curr_char == '}' || curr_char == ']') && !in_string)
        {
            pretty_json.append(1, '\n');
            pretty_json.append((le - 1) * indent, ' ');
            pretty_json.append(1, curr_char);
            return;
        }
        else if (curr_char == ':' && !in_string)
        {
            pretty_json.append(1, curr_char);
            pretty_json.append(1, ' ');
        }
        else if (curr_char == ',' && !in_string)
        {
            pretty_json.append(1, curr_char);
            pretty_json.append(1, '\n');
            pretty_json.append(le * indent, ' ');
        }
        else if (curr_char == '"')
        {
            char last_char = (pos == 0) ? 0 : json_string[pos - 1];
            if (last_char != '\\')
            {
                pretty_json.append(1, curr_char);
                in_string = !in_string;
            }
        }
        else
        {
            pretty_json.append(1, curr_char);
        }
    }
}

maybe this function can help a little

            std::string json_string = "{\"root\":{\"result\":0,\"description\":\"flush success\"},\"config\":{\"result\":0,\"description\":\"flush success\"},\"query\":{\"result\":0,\"description\":\"flush success\"},\"dump\":{\"result\":0,\"description\":\"flush success\"}}";
            std::string pretty_json;
            std::string::size_type pos = 0;
            size_t le = 0;
            prettyJson(pretty_json, json_string, pos, le);

result:

{
    "root": {
        "result": 0,
        "description": "flush success"
    },
    "config": {
        "result": 0,
        "description": "flush success"
    },
    "query": {
        "result": 0,
        "description": "flush success"
    },
    "dump": {
        "result": 0,
        "description": "flush success"
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants