Skip to contentMethod: JsonNode()
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.serialization.model;
16:
17: import cz.cvut.kbss.jsonld.exception.JsonLdSerializationException;
18: import cz.cvut.kbss.jsonld.serialization.JsonGenerator;
19:
20: import java.io.IOException;
21: import java.util.Objects;
22:
23: public abstract class JsonNode {
24:
25: private final String name;
26: private final boolean valueNode;
27:
28: JsonNode() {
29: this.name = null;
30: this.valueNode = true;
31: }
32:
33: public JsonNode(String name) {
34: this.name = Objects.requireNonNull(name);
35: this.valueNode = false;
36: }
37:
38: public String getName() {
39: return name;
40: }
41:
42: public boolean isValueNode() {
43: return valueNode;
44: }
45:
46: public void write(JsonGenerator writer) {
47: try {
48: if (!valueNode) {
49: writeKey(writer);
50: }
51: writeValue(writer);
52: } catch (IOException e) {
53: throw new JsonLdSerializationException("Exception during serialization of node " + this, e);
54: }
55: }
56:
57: void writeKey(JsonGenerator writer) throws IOException {
58: writer.writeFieldName(name);
59: }
60:
61: abstract void writeValue(JsonGenerator writer) throws IOException;
62:
63: @Override
64: public String toString() {
65: return name == null ? "{" : "{\"" + name + "\": ";
66: }
67: }