|
24 | 24 | import java.io.InputStream;
|
25 | 25 | import java.io.InputStreamReader;
|
26 | 26 | import java.io.Reader;
|
| 27 | + |
| 28 | +import java.math.BigDecimal; |
| 29 | +import java.math.BigInteger; |
| 30 | + |
27 | 31 | import java.nio.charset.Charset;
|
| 32 | + |
28 | 33 | import java.util.Iterator;
|
29 | 34 | import java.util.Map;
|
| 35 | +import java.util.Collection; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.Date; |
| 38 | +import java.util.HashMap; |
30 | 39 |
|
31 | 40 | import org.apache.commons.lang3.text.StrSubstitutor;
|
32 | 41 |
|
|
36 | 45 | import com.fasterxml.jackson.databind.ObjectMapper;
|
37 | 46 | import com.fasterxml.jackson.databind.node.ArrayNode;
|
38 | 47 | import com.fasterxml.jackson.databind.node.ObjectNode;
|
| 48 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 49 | +import com.fasterxml.jackson.databind.node.ValueNode; |
| 50 | +import com.fasterxml.jackson.databind.node.NullNode; |
| 51 | +import com.fasterxml.jackson.databind.node.TextNode; |
| 52 | +import com.fasterxml.jackson.databind.node.BooleanNode; |
| 53 | +import com.fasterxml.jackson.databind.node.NumericNode; |
39 | 54 | import com.github.fge.jsonschema.core.exceptions.ProcessingException;
|
40 | 55 | import com.github.fge.jsonschema.core.report.ProcessingMessage;
|
41 | 56 | import com.github.fge.jsonschema.core.report.ProcessingReport;
|
@@ -108,6 +123,126 @@ public static JsonNode json(Reader reader, boolean systemPropertySubstitution) t
|
108 | 123 | return json(bld.toString(), systemPropertySubstitution);
|
109 | 124 | }
|
110 | 125 |
|
| 126 | + /** |
| 127 | + * Returns a Java object for a json value node based on the node type. |
| 128 | + */ |
| 129 | + public static Object valueFromJson(ValueNode node) { |
| 130 | + if (node instanceof NullNode) { |
| 131 | + return null; |
| 132 | + } else { |
| 133 | + if(node instanceof TextNode) { |
| 134 | + return node.textValue(); |
| 135 | + } else if(node instanceof BooleanNode) { |
| 136 | + return node.booleanValue(); |
| 137 | + } else if(node instanceof NumericNode) { |
| 138 | + return node.numberValue(); |
| 139 | + } else { |
| 140 | + throw new RuntimeException("Unsupported node type:"+node.getClass().getName()); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Returns a Json value node for the given value. Dates are converted to strings |
| 147 | + */ |
| 148 | + public static ValueNode valueToJson(Object value) { |
| 149 | + if(value==null) { |
| 150 | + return JsonNodeFactory.instance.nullNode(); |
| 151 | + } else if(value instanceof String) { |
| 152 | + return JsonNodeFactory.instance.textNode((String)value); |
| 153 | + } else if(value instanceof Number) { |
| 154 | + if (value instanceof BigDecimal) { |
| 155 | + return JsonNodeFactory.instance.numberNode((BigDecimal) value); |
| 156 | + } else if (value instanceof BigInteger) { |
| 157 | + return JsonNodeFactory.instance.numberNode((BigInteger) value); |
| 158 | + } else if (value instanceof Double) { |
| 159 | + return JsonNodeFactory.instance.numberNode((Double) value); |
| 160 | + } else if (value instanceof Float) { |
| 161 | + return JsonNodeFactory.instance.numberNode((Float) value); |
| 162 | + } else if (value instanceof Long) { |
| 163 | + return JsonNodeFactory.instance.numberNode((Long) value); |
| 164 | + } else { |
| 165 | + return JsonNodeFactory.instance.numberNode( ((Number)value).intValue()); |
| 166 | + } |
| 167 | + } else if(value instanceof Boolean) { |
| 168 | + return JsonNodeFactory.instance.booleanNode( ((Boolean)value).booleanValue()); |
| 169 | + } else if(value instanceof Date) { |
| 170 | + return JsonNodeFactory.instance.textNode(Constants.getDateFormat().format((Date)value)); |
| 171 | + } else { |
| 172 | + return JsonNodeFactory.instance.textNode(value.toString()); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Converts a json document to an object tree of maps/lists/values |
| 178 | + * |
| 179 | + * If json is a value, then the corresponding Java object is returned. |
| 180 | + * |
| 181 | + * If json is an array, a List is returned. Elements of the list are converted recursively |
| 182 | + * |
| 183 | + * If json is an object, a Map is returned. |
| 184 | + */ |
| 185 | + public static Object fromJson(JsonNode json) { |
| 186 | + if(json==null||json instanceof NullNode) { |
| 187 | + return null; |
| 188 | + } else if(json instanceof ObjectNode) { |
| 189 | + return fromJson((ObjectNode)json); |
| 190 | + } else if(json instanceof ArrayNode) { |
| 191 | + return fromJson((ArrayNode)json); |
| 192 | + } else { |
| 193 | + return valueFromJson( (ValueNode)json); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private static Object fromJson(ObjectNode json) { |
| 198 | + HashMap ret=new HashMap(); |
| 199 | + for(Iterator<Map.Entry<String,JsonNode>> itr=json.fields();itr.hasNext();) { |
| 200 | + Map.Entry<String,JsonNode> entry=itr.next(); |
| 201 | + ret.put(entry.getKey(),fromJson(entry.getValue())); |
| 202 | + } |
| 203 | + return ret; |
| 204 | + } |
| 205 | + |
| 206 | + private static Object fromJson(ArrayNode json) { |
| 207 | + ArrayList ret=new ArrayList(json.size()); |
| 208 | + for(Iterator<JsonNode> itr=json.elements();itr.hasNext();) { |
| 209 | + ret.add(fromJson(itr.next())); |
| 210 | + } |
| 211 | + return ret; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Converts a Java object tree containing maps and collections to json |
| 216 | + */ |
| 217 | + public static JsonNode toJson(Object obj) { |
| 218 | + if(obj==null) { |
| 219 | + return JsonNodeFactory.instance.nullNode(); |
| 220 | + } else if(obj instanceof Map) { |
| 221 | + return toJson( (Map)obj ); |
| 222 | + } else if(obj instanceof Collection) { |
| 223 | + return toJson( (Collection)obj); |
| 224 | + } else { |
| 225 | + return valueToJson(obj); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + private static JsonNode toJson(Map obj) { |
| 230 | + ObjectNode node=JsonNodeFactory.instance.objectNode(); |
| 231 | + for(Iterator<Map.Entry> itr=obj.entrySet().iterator();itr.hasNext();) { |
| 232 | + Map.Entry entry=itr.next(); |
| 233 | + node.set((String)entry.getKey(),toJson(entry.getValue())); |
| 234 | + } |
| 235 | + return node; |
| 236 | + } |
| 237 | + |
| 238 | + private static JsonNode toJson(Collection obj) { |
| 239 | + ArrayNode node=JsonNodeFactory.instance.arrayNode(); |
| 240 | + for(Object x:obj) { |
| 241 | + node.add(toJson(x)); |
| 242 | + } |
| 243 | + return node; |
| 244 | + } |
| 245 | + |
111 | 246 | /**
|
112 | 247 | * Pretty print a json doc
|
113 | 248 | */
|
|
0 commit comments