Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit 5fa7eca

Browse files
authored
Merge pull request #47 from gatling/baseruntime-cleanup
Move specific methods from BaseRuntime to JcfRuntime
2 parents b9bbf68 + 467c7af commit 5fa7eca

File tree

2 files changed

+67
-89
lines changed

2 files changed

+67
-89
lines changed

jmespath-core/src/main/java/io/burt/jmespath/BaseRuntime.java

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.burt.jmespath;
22

33
import java.util.List;
4-
import java.util.Iterator;
54
import java.util.Collection;
65

76
import io.burt.jmespath.parser.ExpressionParser;
@@ -10,7 +9,6 @@
109
import io.burt.jmespath.function.ArgumentTypeException;
1110
import io.burt.jmespath.node.NodeFactory;
1211
import io.burt.jmespath.node.StandardNodeFactory;
13-
import io.burt.jmespath.util.StringEscapeHelper;
1412

1513
/**
1614
* This class can be extended instead of implementing {@link Adapter} directly,
@@ -20,16 +18,6 @@
2018
* these methods if they have more efficient means to perform the same job.
2119
*/
2220
public abstract class BaseRuntime<T> implements Adapter<T> {
23-
private static final StringEscapeHelper jsonEscapeHelper = new StringEscapeHelper(
24-
true,
25-
'b', '\b',
26-
't', '\t',
27-
'n', '\n',
28-
'f', '\f',
29-
'r', '\r',
30-
'\\', '\\',
31-
'\"', '\"'
32-
);
3321

3422
private final FunctionRegistry functionRegistry;
3523
private final NodeFactory<T> nodeFactory;
@@ -173,82 +161,6 @@ public int hashCode() {
173161
return 31;
174162
}
175163

176-
/**
177-
* Helper method to render a value as JSON.
178-
*
179-
* Assumes that <code>null</code>, <code>number</code> and <code>boolean</code>
180-
* render themseves correctly with <code>toString</code>, and that
181-
* <code>string</code> renders itself as an unquoted string.
182-
*/
183-
protected String unparse(T object) {
184-
switch (typeOf(object)) {
185-
case NUMBER:
186-
return unparseNumber(object);
187-
case BOOLEAN:
188-
return unparseBoolean(object);
189-
case NULL:
190-
return unparseNull(object);
191-
case STRING:
192-
return unparseString(object);
193-
case OBJECT:
194-
return unparseObject(object);
195-
case ARRAY:
196-
return unparseArray(object);
197-
default:
198-
throw new IllegalStateException();
199-
}
200-
}
201-
202-
protected String unparseNumber(T object) {
203-
return object.toString();
204-
}
205-
206-
protected String unparseBoolean(T object) {
207-
return object.toString();
208-
}
209-
210-
protected String unparseNull(T object) {
211-
return "null";
212-
}
213-
214-
protected String unparseString(T object) {
215-
return String.format("\"%s\"", escapeString(toString(object)));
216-
}
217-
218-
protected String escapeString(String str) {
219-
return jsonEscapeHelper.escape(str);
220-
}
221-
222-
protected String unparseObject(T object) {
223-
StringBuilder str = new StringBuilder("{");
224-
Iterator<T> keys = getPropertyNames(object).iterator();
225-
while (keys.hasNext()) {
226-
T key = keys.next();
227-
T value = getProperty(object, key);
228-
str.append(unparseString(key));
229-
str.append(':');
230-
str.append(unparse(value));
231-
if (keys.hasNext()) {
232-
str.append(',');
233-
}
234-
}
235-
str.append('}');
236-
return str.toString();
237-
}
238-
239-
protected String unparseArray(T array) {
240-
StringBuilder str = new StringBuilder("[");
241-
Iterator<T> elements = toList(array).iterator();
242-
while (elements.hasNext()) {
243-
str.append(unparse(elements.next()));
244-
if (elements.hasNext()) {
245-
str.append(',');
246-
}
247-
}
248-
str.append(']');
249-
return str.toString();
250-
}
251-
252164
@Override
253165
@Deprecated
254166
public T getProperty(T value, String name) {

jmespath-core/src/main/java/io/burt/jmespath/jcf/JcfRuntime.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,30 @@
44
import java.util.ArrayList;
55
import java.util.Map;
66
import java.util.Collection;
7+
import java.util.Iterator;
78
import java.util.Collections;
89

910
import io.burt.jmespath.BaseRuntime;
1011
import io.burt.jmespath.JmesPathType;
1112
import io.burt.jmespath.RuntimeConfiguration;
13+
import io.burt.jmespath.util.StringEscapeHelper;
1214

1315
import static io.burt.jmespath.JmesPathType.*;
1416

1517
public class JcfRuntime extends BaseRuntime<Object> {
18+
19+
private static final StringEscapeHelper jsonEscapeHelper = new StringEscapeHelper(
20+
true,
21+
'b', '\b',
22+
't', '\t',
23+
'n', '\n',
24+
'f', '\f',
25+
'r', '\r',
26+
'\\', '\\',
27+
'\"', '\"'
28+
);
29+
1630
public JcfRuntime() {
17-
super();
1831
}
1932

2033
public JcfRuntime(RuntimeConfiguration configuration) {
@@ -160,4 +173,57 @@ public Object createNumber(double n) {
160173
public Object createNumber(long n) {
161174
return n;
162175
}
176+
177+
/**
178+
* Helper method to render a value as JSON.
179+
*
180+
* Assumes that <code>null</code>, <code>number</code> and <code>boolean</code>
181+
* render themseves correctly with <code>toString</code>, and that
182+
* <code>string</code> renders itself as an unquoted string.
183+
*/
184+
private String unparse(Object object) {
185+
switch (typeOf(object)) {
186+
case NUMBER:
187+
case BOOLEAN:
188+
return object.toString();
189+
case NULL:
190+
return "null";
191+
case STRING:
192+
return '"' + jsonEscapeHelper.escape(toString(object)) + '"';
193+
case OBJECT:
194+
return unparseObject(object);
195+
case ARRAY:
196+
return unparseArray(object);
197+
default:
198+
throw new IllegalStateException();
199+
}
200+
}
201+
202+
private String unparseObject(Object object) {
203+
StringBuilder str = new StringBuilder("{");
204+
Collection<Object> propertyNames = getPropertyNames(object);
205+
for (Object key: propertyNames) {
206+
Object value = getProperty(object, key);
207+
str.append('"').append(jsonEscapeHelper.escape(toString(key))).append("\":");
208+
str.append(unparse(value));
209+
str.append(',');
210+
}
211+
if (!propertyNames.isEmpty()) {
212+
str.setLength(str.length() - 1);
213+
}
214+
return str.append('}').toString();
215+
}
216+
217+
private String unparseArray(Object array) {
218+
StringBuilder str = new StringBuilder("[");
219+
List<Object> elements = toList(array);
220+
for (Object element : elements) {
221+
str.append(unparse(element)).append(',');
222+
}
223+
if (!elements.isEmpty()) {
224+
str.setLength(str.length() - 1);
225+
226+
}
227+
return str.append(']').toString();
228+
}
163229
}

0 commit comments

Comments
 (0)