blob: 7fa8cddea09e42dd70382277cc708684eebb20a5 (
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.hitfield;
import com.yahoo.data.access.Inspectable;
import com.yahoo.data.access.Inspector;
import com.yahoo.data.access.Type;
import com.yahoo.data.access.simple.Value;
import com.yahoo.data.access.slime.SlimeAdapter;
import com.yahoo.prelude.query.WeightedSetItem;
import com.yahoo.slime.JsonDecoder;
import com.yahoo.slime.Slime;
import com.yahoo.text.Utf8;
/**
* A JSON wrapper. Contains XML-style rendering of a JSON structure.
*
* @author Steinar Knutsen
*/
public class JSONString implements Inspectable {
private Inspector value;
private String content;
private boolean didInitContent = false;
public JSONString(Inspector value) {
if (value == null) {
throw new IllegalArgumentException("JSONString does not accept null value.");
}
this.value = value;
}
public Inspector inspect() {
if (value == null) {
JsonDecoder decoder = new JsonDecoder();
Slime slime = decoder.decode(new Slime(), Utf8.toBytes(content));
if (slime.get().field("error_message").valid() &&
slime.get().field("partial_result").valid() &&
slime.get().field("offending_input").valid())
{
// probably a json parse error...
value = new Value.StringValue(content);
} else if (slime.get().type() == com.yahoo.slime.Type.OBJECT ||
slime.get().type() == com.yahoo.slime.Type.ARRAY)
{
// valid json object or array
value = new SlimeAdapter(slime.get());
} else {
// 'valid' json, but leaf value
value = new Value.StringValue(content);
}
}
return value;
}
private void initContent() {
if (didInitContent) {
return;
}
didInitContent = true;
if (value.type() == Type.EMPTY) {
content = "";
} else if (value.type() == Type.STRING) {
content = value.asString();
} else {
// This will be json, because we know there is Slime below
content = value.toString();
}
}
/**
* @throws IllegalArgumentException Does not accept null content
*/
public JSONString(String content) {
if (content == null) {
throw new IllegalArgumentException("JSONString does not accept null content.");
}
this.content = content;
didInitContent = true;
}
@Override
public String toString() {
if (value != null) {
return renderFromInspector();
}
initContent();
if (content.length() == 0) {
return content;
}
inspect();
return renderFromInspector();
}
public boolean fillWeightedSetItem(WeightedSetItem set) {
initContent();
inspect();
if (value.type() != Type.ARRAY) return false;
for (Inspector item : value.entries()) {
if (item.entryCount() != 2) return false;
if (item.entry(0).type() != Type.STRING) return false;
if (item.entry(1).type() != Type.LONG && item.entry(1).type() != Type.DOUBLE) return false;
set.addToken(item.entry(0).asString(), (int) item.entry(1).asLong());
}
return true;
}
public String getContent() {
initContent();
return content;
}
public String renderFromInspector() {
return XmlRenderer.render(new StringBuilder(), value).toString();
}
}
|