Skip to content

Commit 9ee732d

Browse files
committed
move default value of NotFoundAny
1 parent b7f67a9 commit 9ee732d

2 files changed

Lines changed: 36 additions & 30 deletions

File tree

src/main/java/com/jsoniter/any/Any.java

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -130,71 +130,47 @@ public <T> T as(TypeLiteral<T> typeLiteral) {
130130
}
131131

132132
public final boolean toBoolean(Object... keys) {
133-
Any found = get(keys);
134-
if (found.valueType() == ValueType.INVALID) {
135-
return false;
136-
}
137-
return found.toBoolean();
133+
return get(keys).toBoolean();
138134
}
139135

140136
public boolean toBoolean() {
141137
throw reportUnexpectedType(ValueType.BOOLEAN);
142138
}
143139

144140
public final int toInt(Object... keys) {
145-
Any found = get(keys);
146-
if (found.valueType() == ValueType.INVALID) {
147-
return 0;
148-
}
149-
return found.toInt();
141+
return get(keys).toInt();
150142
}
151143

152144
public int toInt() {
153145
throw reportUnexpectedType(ValueType.NUMBER);
154146
}
155147

156148
public final long toLong(Object... keys) {
157-
Any found = get(keys);
158-
if (found.valueType() == ValueType.INVALID) {
159-
return 0;
160-
}
161-
return found.toLong();
149+
return get(keys).toLong();
162150
}
163151

164152
public long toLong() {
165153
throw reportUnexpectedType(ValueType.NUMBER);
166154
}
167155

168156
public final float toFloat(Object... keys) {
169-
Any found = get(keys);
170-
if (found.valueType() == ValueType.INVALID) {
171-
return 0;
172-
}
173-
return found.toFloat();
157+
return get(keys).toFloat();
174158
}
175159

176160
public float toFloat() {
177161
throw reportUnexpectedType(ValueType.NUMBER);
178162
}
179163

180164
public final double toDouble(Object... keys) {
181-
Any found = get(keys);
182-
if (found.valueType() == ValueType.INVALID) {
183-
return 0;
184-
}
185-
return found.toDouble();
165+
return get(keys).toDouble();
186166
}
187167

188168
public double toDouble() {
189169
throw reportUnexpectedType(ValueType.NUMBER);
190170
}
191171

192172
public final String toString(Object... keys) {
193-
Any found = get(keys);
194-
if (found.valueType() == ValueType.INVALID) {
195-
return "";
196-
}
197-
return found.toString();
173+
return get(keys).toString();
198174
}
199175

200176

src/main/java/com/jsoniter/any/NotFoundAny.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,34 @@ public Any get(Object key) {
5555
public Any get(Object[] keys, int idx) {
5656
return this;
5757
}
58+
59+
@Override
60+
public boolean toBoolean() {
61+
return false;
62+
}
63+
64+
@Override
65+
public int toInt() {
66+
return 0;
67+
}
68+
69+
@Override
70+
public long toLong() {
71+
return 0;
72+
}
73+
74+
@Override
75+
public float toFloat() {
76+
return 0;
77+
}
78+
79+
@Override
80+
public double toDouble() {
81+
return 0;
82+
}
83+
84+
@Override
85+
public String toString() {
86+
return "";
87+
}
5888
}

0 commit comments

Comments
 (0)