forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringLazyAny.java
More file actions
136 lines (121 loc) · 3.2 KB
/
StringLazyAny.java
File metadata and controls
136 lines (121 loc) · 3.2 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.jsoniter.any;
import com.jsoniter.CodegenAccess;
import com.jsoniter.JsonIterator;
import com.jsoniter.JsonIteratorPool;
import com.jsoniter.ValueType;
import com.jsoniter.spi.JsonException;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
class StringLazyAny extends LazyAny {
private final static String FALSE = "false";
private String cache;
public StringLazyAny(byte[] data, int head, int tail) {
super(data, head, tail);
}
@Override
public ValueType valueType() {
return ValueType.STRING;
}
@Override
public Object object() {
fillCache();
return cache;
}
@Override
public boolean toBoolean() {
fillCache();
int len = cache.length();
if (len == 0) {
return false;
}
if (len == 5 && FALSE.equals(cache)) {
return false;
}
for (int i = 0; i < len; i++) {
switch (cache.charAt(i)) {
case ' ':
case '\t':
case '\n':
case '\r':
continue;
default:
return true;
}
}
return false;
}
@Override
public int toInt() {
JsonIterator iter = parse();
try {
CodegenAccess.nextToken(iter);
return iter.readInt();
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
@Override
public long toLong() {
JsonIterator iter = parse();
try {
CodegenAccess.nextToken(iter);
return iter.readLong();
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
@Override
public float toFloat() {
JsonIterator iter = parse();
try {
CodegenAccess.nextToken(iter);
return iter.readFloat();
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
@Override
public double toDouble() {
JsonIterator iter = parse();
try {
CodegenAccess.nextToken(iter);
return iter.readDouble();
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
@Override
public BigInteger toBigInteger() {
return new BigInteger(toString());
}
@Override
public BigDecimal toBigDecimal() {
return new BigDecimal(toString());
}
@Override
public String toString() {
fillCache();
return cache;
}
private void fillCache() {
if (cache == null) {
JsonIterator iter = parse();
try {
cache = iter.readString();
} catch (IOException e) {
throw new JsonException();
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
}
}