aboutsummaryrefslogtreecommitdiffstats
path: root/testutil/src/main/java/com/yahoo/test/json/JsonNodeFormatter.java
blob: 8e7b23c7f93f899f82f024119ceda8e43b87d34e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.test.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

/**
 * Formats a {@link JsonNode} to a normalized JSON string, see {@link JsonTestHelper}.
 *
 * @author hakonhall
 */
class JsonNodeFormatter {
    private final JsonBuilder builder;

    /** See {@link JsonTestHelper}. */
    static String toNormalizedJson(JsonNode jsonNode, boolean compact) {
        JsonNodeFormatter formatter = new JsonNodeFormatter(compact);
        formatter.appendValue(jsonNode);
        return formatter.toString();
    }

    private JsonNodeFormatter(boolean compact) {
        builder = compact ? JsonBuilder.forCompactJson() : JsonBuilder.forMultiLineJson(2);
    }

    private void appendValue(JsonNode jsonNode) {
        switch (jsonNode.getNodeType()) {
            case OBJECT -> {
                ObjectNode objectNode = (ObjectNode) jsonNode;
                ArrayList<String> fieldNames = new ArrayList<>();
                objectNode.fieldNames().forEachRemaining(fieldNames::add);
                Collections.sort(fieldNames);
                if (fieldNames.isEmpty()) {
                    builder.append("{}");
                } else {
                    boolean firstIteration = true;
                    for (var fieldName : fieldNames) {
                        if (firstIteration) {
                            builder.appendLineAndIndent("{", +1);
                            firstIteration = false;
                        } else {
                            builder.appendLineAndIndent(",");
                        }

                        builder.appendStringValue(fieldName);
                        builder.appendColon();
                        appendValue(objectNode.get(fieldName));
                    }

                    builder.newLineIndentAndAppend(-1, "}");
                }
            }
            case ARRAY -> {
                Iterator<JsonNode> elements = jsonNode.elements();
                if (elements.hasNext()) {
                    builder.appendLineAndIndent("[", +1);
                    appendValue(elements.next());

                    while (elements.hasNext()) {
                        builder.appendLineAndIndent(",");
                        appendValue(elements.next());
                    }

                    builder.newLineIndentAndAppend(-1, "]");
                } else {
                    builder.append("[]");
                }
            }
            case BOOLEAN, NUMBER, NULL -> builder.append(jsonNode.asText());
            case STRING -> builder.appendStringValue(jsonNode.asText());
            case BINARY, MISSING, POJO -> throw new IllegalStateException(jsonNode.getNodeType().toString());
        }
    }

    @Override
    public String toString() { return builder.toString(); }
}