| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util.json; |
| 7 |
|
| 8 |
import java.math.BigDecimal; |
| 9 |
import java.util.ArrayDeque; |
| 10 |
|
| 11 |
/** |
| 12 |
* JSON value target. |
| 13 |
*/ |
| 14 |
public final class JSONValueTarget extends JSONTarget<JSONValue> { |
| 15 |
|
| 16 |
private final ArrayDeque<JSONValue> stack; |
| 17 |
|
| 18 |
private final ArrayDeque<String> names; |
| 19 |
|
| 20 |
private boolean needSeparator; |
| 21 |
|
| 22 |
private String memberName; |
| 23 |
|
| 24 |
private JSONValue result; |
| 25 |
|
| 26 |
/** |
| 27 |
* Creates new instance of JSON value target. |
| 28 |
*/ |
| 29 |
public JSONValueTarget() { |
| 30 |
stack = new ArrayDeque<>(); |
| 31 |
names = new ArrayDeque<>(); |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
public void startObject() { |
| 36 |
beforeValue(); |
| 37 |
names.push(memberName != null ? memberName : ""); |
| 38 |
memberName = null; |
| 39 |
stack.push(new JSONObject()); |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public void endObject() { |
| 44 |
if (memberName != null) { |
| 45 |
throw new IllegalStateException(); |
| 46 |
} |
| 47 |
JSONValue value = stack.poll(); |
| 48 |
if (!(value instanceof JSONObject)) { |
| 49 |
throw new IllegalStateException(); |
| 50 |
} |
| 51 |
memberName = names.pop(); |
| 52 |
afterValue(value); |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public void startArray() { |
| 57 |
beforeValue(); |
| 58 |
names.push(memberName != null ? memberName : ""); |
| 59 |
memberName = null; |
| 60 |
stack.push(new JSONArray()); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void endArray() { |
| 65 |
JSONValue value = stack.poll(); |
| 66 |
if (!(value instanceof JSONArray)) { |
| 67 |
throw new IllegalStateException(); |
| 68 |
} |
| 69 |
memberName = names.pop(); |
| 70 |
afterValue(value); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void member(String name) { |
| 75 |
if (memberName != null || !(stack.peek() instanceof JSONObject)) { |
| 76 |
throw new IllegalStateException(); |
| 77 |
} |
| 78 |
memberName = name; |
| 79 |
beforeValue(); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void valueNull() { |
| 84 |
beforeValue(); |
| 85 |
afterValue(JSONNull.NULL); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void valueFalse() { |
| 90 |
beforeValue(); |
| 91 |
afterValue(JSONBoolean.FALSE); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void valueTrue() { |
| 96 |
beforeValue(); |
| 97 |
afterValue(JSONBoolean.TRUE); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void valueNumber(BigDecimal number) { |
| 102 |
beforeValue(); |
| 103 |
afterValue(new JSONNumber(number)); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void valueString(String string) { |
| 108 |
beforeValue(); |
| 109 |
afterValue(new JSONString(string)); |
| 110 |
} |
| 111 |
|
| 112 |
private void beforeValue() { |
| 113 |
if (memberName == null && stack.peek() instanceof JSONObject) { |
| 114 |
throw new IllegalStateException(); |
| 115 |
} |
| 116 |
if (needSeparator) { |
| 117 |
if (stack.isEmpty()) { |
| 118 |
throw new IllegalStateException(); |
| 119 |
} |
| 120 |
needSeparator = false; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
private void afterValue(JSONValue value) { |
| 125 |
JSONValue parent = stack.peek(); |
| 126 |
if (parent == null) { |
| 127 |
result = value; |
| 128 |
} else if (parent instanceof JSONObject) { |
| 129 |
((JSONObject) parent).addMember(memberName, value); |
| 130 |
} else { |
| 131 |
((JSONArray) parent).addElement(value); |
| 132 |
} |
| 133 |
needSeparator = true; |
| 134 |
memberName = null; |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public boolean isPropertyExpected() { |
| 139 |
return memberName == null && stack.peek() instanceof JSONObject; |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public boolean isValueSeparatorExpected() { |
| 144 |
return needSeparator; |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public JSONValue getResult() { |
| 149 |
if (!stack.isEmpty() || result == null) { |
| 150 |
throw new IllegalStateException(); |
| 151 |
} |
| 152 |
return result; |
| 153 |
} |
| 154 |
|
| 155 |
} |