You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2017/11/29 14:51:51 UTC
[camel] 04/08: Replaced org.json library with a self-written JSON
parser due to imcompatibilities with the Apache 2.0 licence
This is an automated email from the ASF dual-hosted git repository.
acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2cdaa43634ab1c5c58ef5c6720064d04bbea61ea
Author: Roman Vottner <ro...@gmx.at>
AuthorDate: Wed Nov 1 13:45:11 2017 +0100
Replaced org.json library with a self-written JSON parser due to imcompatibilities with the Apache 2.0 licence
---
components/camel-aws-xray/pom.xml | 6 +-
.../camel/component/aws/xray/FakeAWSDaemon.java | 85 +++++-----
.../camel/component/aws/xray/json/JsonArray.java | 23 +++
.../camel/component/aws/xray/json/JsonObject.java | 93 +++++++++++
.../camel/component/aws/xray/json/JsonParser.java | 154 ++++++++++++++++++
.../component/aws/xray/json/JsonStructure.java | 22 +++
.../camel/component/aws/xray/json/JsonTest.java | 174 +++++++++++++++++++++
7 files changed, 510 insertions(+), 47 deletions(-)
diff --git a/components/camel-aws-xray/pom.xml b/components/camel-aws-xray/pom.xml
index b440d7d..c859aaa 100644
--- a/components/camel-aws-xray/pom.xml
+++ b/components/camel-aws-xray/pom.xml
@@ -91,9 +91,9 @@
<scope>test</scope>
</dependency>
<dependency>
- <groupId>org.json</groupId>
- <artifactId>json</artifactId>
- <version>20170516</version>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.6</version>
<scope>test</scope>
</dependency>
diff --git a/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/FakeAWSDaemon.java b/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/FakeAWSDaemon.java
index 676929d..4e8ea91 100644
--- a/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/FakeAWSDaemon.java
+++ b/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/FakeAWSDaemon.java
@@ -28,13 +28,15 @@ import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
+
import org.apache.camel.component.aws.xray.TestDataBuilder.TestEntity;
import org.apache.camel.component.aws.xray.TestDataBuilder.TestSegment;
import org.apache.camel.component.aws.xray.TestDataBuilder.TestSubsegment;
import org.apache.camel.component.aws.xray.TestDataBuilder.TestTrace;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
+import org.apache.camel.component.aws.xray.json.JsonArray;
+import org.apache.camel.component.aws.xray.json.JsonObject;
+import org.apache.camel.component.aws.xray.json.JsonParser;
+import org.apache.commons.lang.StringUtils;
import org.junit.rules.ExternalResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -95,8 +97,8 @@ public class FakeAWSDaemon extends ExternalResource {
serverSocket = new DatagramSocket(2000);
StringBuilder sb = new StringBuilder();
- byte[] receiveData = new byte[8096];
while (!done) {
+ byte[] receiveData = new byte[2048];
DatagramPacket receivedPacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivedPacket);
@@ -105,17 +107,26 @@ public class FakeAWSDaemon extends ExternalResource {
String locSegment = null;
try {
- String raw = sb.toString();
+ String raw = sb.toString().trim();
String[] segments = raw.split("\\n");
for (String segment : segments) {
locSegment = segment;
LOG.trace("Processing received segment: {}", segment);
if (!"".equals(segment)) {
+ if (!segment.endsWith("}")
+ || StringUtils.countMatches(segment, "{") != StringUtils.countMatches(segment, "}")
+ || StringUtils.countMatches(segment, "[") != StringUtils.countMatches(segment, "]")) {
+ LOG.trace("Skipping incomplete content: {}", segment);
+ continue;
+ }
if (segment.contains("format") && segment.contains("version")) {
LOG.trace("Skipping format and version JSON");
} else {
LOG.trace("Converting segment {} to a Java object", segment);
- JSONObject json = new JSONObject(segment);
+ // clean the JSON string received
+ LOG.trace("Original JSON content: {}", segment);
+ locSegment = segment;
+ JsonObject json = (JsonObject) JsonParser.parse(segment);
String traceId = json.getString("trace_id");
TestTrace testTrace = receivedTraces.get(traceId);
if (null == testTrace) {
@@ -131,8 +142,8 @@ public class FakeAWSDaemon extends ExternalResource {
}
}
LOG.trace("Item {} received. JSON content: {}, Raw: {}",
- receivedTraces.size(), receivedTraces, raw);
- } catch (JSONException jsonEx) {
+ receivedTraces.size(), receivedTraces, raw);
+ } catch (Exception jsonEx) {
LOG.warn("Could not convert segment " + locSegment + " to a Java object", jsonEx);
}
}
@@ -143,12 +154,12 @@ public class FakeAWSDaemon extends ExternalResource {
}
}
- private TestSegment convertData(JSONObject json) {
+ private TestSegment convertData(JsonObject json) {
String name = json.getString("name");
double startTime = json.getDouble("start_time");
TestSegment segment = new TestSegment(name, startTime);
if (json.has("subsegments")) {
- JSONArray jsonSubsegments = json.getJSONArray("subsegments");
+ JsonArray jsonSubsegments = (JsonArray) json.get("subsegments");
List<TestSubsegment> subsegments = convertSubsegments(jsonSubsegments);
for (TestSubsegment subsegment : subsegments) {
segment.withSubsegment(subsegment);
@@ -159,19 +170,19 @@ public class FakeAWSDaemon extends ExternalResource {
return segment;
}
- private List<TestSubsegment> convertSubsegments(JSONArray jsonSubsegments) {
- List<TestSubsegment> subsegments = new ArrayList<>(jsonSubsegments.length());
- for (int i = 0; i < jsonSubsegments.length(); i++) {
- JSONObject jsonSubsegment = jsonSubsegments.getJSONObject(i);
+ private List<TestSubsegment> convertSubsegments(JsonArray jsonSubsegments) {
+ List<TestSubsegment> subsegments = new ArrayList<>(jsonSubsegments.size());
+ for (int i = 0; i < jsonSubsegments.size(); i++) {
+ JsonObject jsonSubsegment = jsonSubsegments.toArray(new JsonObject[jsonSubsegments.size()])[i];
subsegments.add(convertSubsegment(jsonSubsegment));
}
return subsegments;
}
- private TestSubsegment convertSubsegment(JSONObject json) {
- TestSubsegment subsegment = new TestSubsegment(json.getString("name"));
+ private TestSubsegment convertSubsegment(JsonObject json) {
+ TestSubsegment subsegment = new TestSubsegment((String)json.get("name"));
if (json.has("subsegments")) {
- List<TestSubsegment> subsegments = convertSubsegments(json.getJSONArray("subsegments"));
+ List<TestSubsegment> subsegments = convertSubsegments((JsonArray) json.get("subsegments"));
for (TestSubsegment tss : subsegments) {
subsegment.withSubsegment(tss);
}
@@ -181,18 +192,19 @@ public class FakeAWSDaemon extends ExternalResource {
return subsegment;
}
- private void addAnnotationsIfAvailable(TestEntity<?> entity, JSONObject json) {
+ private void addAnnotationsIfAvailable(TestEntity<?> entity, JsonObject json) {
if (json.has("annotations")) {
- Map<String, Object> annotations = parseAnnotations(json.getJSONObject("annotations"));
- for (String key : annotations.keySet()) {
- entity.withAnnotation(key, annotations.get(key));
+ JsonObject annotations = (JsonObject) json.get("annotations");
+ for (String key : annotations.getKeys()) {
+ entity.withAnnotation((String)key, annotations.get(key));
}
}
}
- private void addMetadataIfAvailable(TestEntity<?> entity, JSONObject json) {
+ private void addMetadataIfAvailable(TestEntity<?> entity, JsonObject json) {
if (json.has("metadata")) {
- Map<String, Map<String, Object>> metadata = parseMetadata(json.getJSONObject("metadata"));
+ JsonObject rawMetadata = (JsonObject) json.get("metadata");
+ Map<String, Map<String, Object>> metadata = parseMetadata(rawMetadata);
for (String namespace : metadata.keySet()) {
for (String key : metadata.get(namespace).keySet()) {
entity.withMetadata(namespace, key, metadata.get(namespace).get(key));
@@ -201,22 +213,7 @@ public class FakeAWSDaemon extends ExternalResource {
}
}
- private Map<String, Object> parseAnnotations(JSONObject json) {
- /*
- "annotations" : {
- "test2" : 1,
- "test3" : true,
- "test1" : "test"
- }
- */
- Map<String, Object> annotations = new LinkedHashMap<>(json.keySet().size());
- for (String key : json.keySet()) {
- annotations.put(key, json.get(key));
- }
- return annotations;
- }
-
- private Map<String, Map<String, Object>> parseMetadata(JSONObject json) {
+ private Map<String, Map<String, Object>> parseMetadata(JsonObject json) {
/*
"metadata" : {
"default" : {
@@ -227,13 +224,13 @@ public class FakeAWSDaemon extends ExternalResource {
}
}
*/
- Map<String, Map<String, Object>> metadata = new LinkedHashMap<>(json.keySet().size());
- for (String namespace : json.keySet()) {
- JSONObject namespaceData = json.getJSONObject(namespace);
+ Map<String, Map<String, Object>> metadata = new LinkedHashMap<>(json.getKeys().size());
+ for (String namespace : json.getKeys()) {
+ JsonObject namespaceData = (JsonObject) json.get(namespace);
if (!metadata.containsKey(namespace)) {
- metadata.put(namespace, new LinkedHashMap<>(namespaceData.keySet().size()));
+ metadata.put(namespace, new LinkedHashMap<>(namespaceData.getKeys().size()));
}
- for (String key : namespaceData.keySet()) {
+ for (String key : namespaceData.getKeys()) {
metadata.get(namespace).put(key, namespaceData.get(key));
}
}
diff --git a/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonArray.java b/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonArray.java
new file mode 100644
index 0000000..3d00ddc
--- /dev/null
+++ b/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonArray.java
@@ -0,0 +1,23 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.aws.xray.json;
+
+import java.util.ArrayList;
+
+public class JsonArray extends ArrayList<JsonStructure> implements JsonStructure {
+
+}
diff --git a/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonObject.java b/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonObject.java
new file mode 100644
index 0000000..6698607
--- /dev/null
+++ b/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonObject.java
@@ -0,0 +1,93 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.aws.xray.json;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+public class JsonObject implements JsonStructure {
+
+ private Map<String, Object> data = new LinkedHashMap<>();
+
+ void addElement(String key, Object value) {
+ data.put(key, value);
+ }
+
+ public Object get(String key) {
+ return data.get(key);
+ }
+
+ public Set<String> getKeys() {
+ return data.keySet();
+ }
+
+ public boolean has(String key) {
+ return data.containsKey(key);
+ }
+
+ public String getString(String key) {
+ if (data.containsKey(key) && null != data.get(key)) {
+ return data.get(key).toString();
+ }
+ return null;
+ }
+
+ public Double getDouble(String key) {
+ if (data.containsKey(key) && null != data.get(key)) {
+ Object value = data.get(key);
+ if (value instanceof String) {
+ return Double.valueOf((String) value);
+ }
+ return (Double) value;
+ }
+ return 0D;
+ }
+
+ public Long getLong(String key) {
+ if (data.containsKey(key) && null != data.get(key)) {
+ Object value = data.get(key);
+ if (value instanceof String) {
+ return Long.valueOf((String) value);
+ }
+ return (Long) value;
+ }
+ return 0L;
+ }
+
+ public Integer getInteger(String key) {
+ if (data.containsKey(key) && null != data.get(key)) {
+ Object value = data.get(key);
+ if (value instanceof String) {
+ return Integer.valueOf((String) value);
+ }
+ return (Integer) value;
+ }
+ return 0;
+ }
+
+ public Boolean getBoolean(String key) {
+ if (data.containsKey(key) && null != data.get(key)) {
+ Object value = data.get(key);
+ if (value instanceof String) {
+ return Boolean.valueOf((String) value);
+ }
+ return (Boolean) value;
+ }
+ return null;
+ }
+}
diff --git a/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonParser.java b/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonParser.java
new file mode 100644
index 0000000..13acf93
--- /dev/null
+++ b/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonParser.java
@@ -0,0 +1,154 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.aws.xray.json;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Stack;
+
+import org.apache.commons.lang.StringUtils;
+
+public final class JsonParser {
+
+ private JsonParser() {
+
+ }
+
+ public static JsonStructure parse(final String jsonString) {
+ String json = jsonString.replaceAll("\n", "");
+
+ Stack<JsonStructure> stack = new Stack<>();
+
+ JsonStructure ret = null;
+ List<String> doNotIncludeSymbols = Arrays.asList(",", ":", "\"");
+ StringBuilder curToken = new StringBuilder();
+ String keyName = null;
+ boolean inWord = false;
+ for (char c : json.toCharArray()) {
+ // CHECKSTYLE:OFF
+ // fallthrough is intended here and as this is only a helper class for tests (as the previously used
+ // org.json classes are incompatible with Apache 2.0 license) formatting rules shouldn't be that strict IMO
+ // Note that the fall-through was the only rant checkstyle generated, so everything else should follow these
+ // guidelines
+ switch (c) {
+ case '{':
+ if (!inWord) {
+ // JsonObject begin
+ JsonObject newNode = new JsonObject();
+ addJson(newNode, keyName, stack);
+ keyName = null;
+ stack.push(newNode);
+ break;
+ }
+ case '}':
+ if (!inWord) {
+ // JsonObject end
+ if (!stack.isEmpty()) {
+ ret = stack.pop();
+ }
+ if (keyName != null) {
+ if (ret instanceof JsonObject) {
+ ((JsonObject) ret).addElement(sanitizeKey(keyName), sanitizeData(curToken.toString()));
+ keyName = null;
+ curToken.delete(0, curToken.length());
+ }
+ }
+ break;
+ }
+ case '[':
+ if (!inWord) {
+ // JsonArray start
+ JsonArray newArray = new JsonArray();
+ addJson(newArray, keyName, stack);
+ keyName = null;
+ stack.push(newArray);
+ break;
+ }
+ case ']':
+ if (!inWord) {
+ // JsonArray end
+ if (!stack.isEmpty()) {
+ ret = stack.pop();
+ }
+ break;
+ }
+ case ':':
+ if (!inWord) {
+ // Element start
+ keyName = curToken.toString();
+ curToken.delete(0, curToken.length());
+ break;
+ }
+ case ',':
+ if (!inWord) {
+ // Element separator
+ if (keyName != null) {
+ JsonObject jsonObj = (JsonObject) stack.peek();
+ jsonObj.addElement(sanitizeKey(keyName), sanitizeData(curToken.toString()));
+ }
+ curToken.delete(0, curToken.length());
+ keyName = null;
+ break;
+ }
+ default:
+ if (('"' == c && curToken.length() == 0)
+ || ('"' == c && curToken.length() > 0 && curToken.charAt(curToken.length() - 1) != '\\')) {
+ inWord = !inWord;
+ }
+ if (!inWord && !doNotIncludeSymbols.contains("" + c)) {
+ curToken.append(c);
+ } else if ('"' != c || (curToken.length() > 0 && curToken.charAt(curToken.length() - 1) == '\\')) {
+ curToken.append(c);
+ }
+ }
+ // CHECKSTYLE:ON
+ }
+ return ret;
+ }
+
+ private static void addJson(JsonStructure element, String key, Stack<JsonStructure> stack) {
+ if (!stack.isEmpty()) {
+ JsonStructure json = stack.peek();
+ if (json instanceof JsonObject && key != null) {
+ ((JsonObject)json).addElement(sanitizeKey(key), element);
+ } else if (json instanceof JsonArray) {
+ ((JsonArray)json).add(element);
+ }
+ }
+ }
+
+ private static String sanitizeKey(String key) {
+ return key.trim();
+ }
+
+ private static Object sanitizeData(String data) {
+ data = data.trim();
+ if (data.toLowerCase().equals("true") || data.toLowerCase().equals("false")) {
+ return Boolean.valueOf(data);
+ }
+ if (data.contains(".") && StringUtils.countMatches(data, ".") == 1 && data.matches("[0-9\\.]+")) {
+ return Double.valueOf(data);
+ } else if (data.matches("[0-9]+")) {
+ try {
+ return Integer.valueOf(data);
+ } catch (NumberFormatException nfEx) {
+ return Long.valueOf(data);
+ }
+ }
+ return data;
+ }
+}
diff --git a/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonStructure.java b/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonStructure.java
new file mode 100644
index 0000000..81daa3a
--- /dev/null
+++ b/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonStructure.java
@@ -0,0 +1,22 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.aws.xray.json;
+
+interface JsonStructure {
+
+
+}
diff --git a/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonTest.java b/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonTest.java
new file mode 100644
index 0000000..10c5138
--- /dev/null
+++ b/components/camel-aws-xray/src/test/java/org/apache/camel/component/aws/xray/json/JsonTest.java
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.aws.xray.json;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.assertThat;
+
+public class JsonTest {
+
+ @Test
+ public void testJsonParse() {
+ JsonStructure json = JsonParser.parse("{\n"
+ + " \"test\": \"some string\",\n"
+ + " \"otherKey\": true,\n"
+ + " \"nextKey\": 1234,\n"
+ + " \"doubleKey\": 1234.567,\n"
+ + " \"subElement\": {\n"
+ + " \"subKey\": \"some other string\",\n"
+ + " \"complexString\": \"String with JSON syntax elements like .,\\\" { or }\"\n"
+ + " },\n"
+ + " \"arrayElement\": [\n"
+ + " {\n"
+ + " \"id\": 1,\n"
+ + " \"name\": \"test1\"\n"
+ + " },\n"
+ + " {\n"
+ + " \"id\": 2,\n"
+ + " \"name\": \"test2\"\n"
+ + " }\n"
+ + " ]\n"
+ + "}");
+
+ assertThat(json, is(notNullValue()));
+ assertThat(json, is(instanceOf(JsonObject.class)));
+ JsonObject jsonObj = (JsonObject) json;
+ assertThat(jsonObj.getKeys().size(), is(equalTo(6)));
+ assertThat(jsonObj.getString("test"), is(equalTo("some string")));
+ assertThat(jsonObj.getBoolean("otherKey"), is(equalTo(true)));
+ assertThat(jsonObj.getInteger("nextKey"), is(equalTo(1234)));
+ assertThat(jsonObj.getDouble("doubleKey"), is(equalTo(1234.567)));
+ assertThat(jsonObj.get("subElement"), is(instanceOf(JsonObject.class)));
+ JsonObject jsonSub = (JsonObject) jsonObj.get("subElement");
+ assertThat(jsonSub.getString("subKey"), is(equalTo("some other string")));
+ assertThat(jsonSub.getString("complexString"), is(equalTo("String with JSON syntax elements like .,\\\" { or }")));
+ assertThat(jsonObj.get("arrayElement"), is(instanceOf(JsonArray.class)));
+ JsonArray jsonArr = (JsonArray) jsonObj.get("arrayElement");
+ assertThat(jsonArr.size(), is(equalTo(2)));
+ assertThat(jsonArr.get(0), is(instanceOf(JsonObject.class)));
+ JsonObject arrElem0 = (JsonObject) jsonArr.get(0);
+ assertThat(arrElem0.getInteger("id"), is(equalTo(1)));
+ assertThat(arrElem0.getString("name"), is(equalTo("test1")));
+ assertThat(jsonArr.get(1), is(instanceOf(JsonObject.class)));
+ JsonObject arrElem1 = (JsonObject) jsonArr.get(1);
+ assertThat(arrElem1.getInteger("id"), is(equalTo(2)));
+ assertThat(arrElem1.getString("name"), is(equalTo("test2")));
+ }
+
+ @Test
+ public void testJsonParseSample() {
+
+ JsonStructure json = JsonParser.parse("{"
+ + " \"name\":\"b\","
+ + " \"id\":\"6ae1778525198ce8\","
+ + " \"start_time\":1.50947752281E9,"
+ + " \"trace_id\":\"1-59f8cc92-4819a77b4109de34405a5643\","
+ + " \"end_time\":1.50947752442E9,"
+ + " \"aws\":{"
+ + " \"xray\":{"
+ + " \"sdk_version\":\"1.2.0\","
+ + " \"sdk\":\"X-Ray for Java\""
+ + " }"
+ + " },"
+ + " \"service\":{"
+ + " \"runtime\":\"Java HotSpot(TM) 64-Bit Server VM\","
+ + " \"runtime_version\":\"1.8.0_144\""
+ + " }"
+ + "}"
+ + "}");
+
+ assertThat(json, is(notNullValue()));
+ JsonObject jsonObj = (JsonObject) json;
+ assertThat(jsonObj.getKeys().size(), is(equalTo(7)));
+ assertThat(jsonObj.getString("name"), is(equalTo("b")));
+ assertThat(jsonObj.getString("id"), is(equalTo("6ae1778525198ce8")));
+ assertThat(jsonObj.getString("trace_id"), is(equalTo("1-59f8cc92-4819a77b4109de34405a5643")));
+ assertThat(jsonObj.getDouble("start_time"), is(equalTo(1.50947752281E9)));
+ assertThat(jsonObj.getDouble("end_time"), is(equalTo(1.50947752442E9)));
+ assertThat(jsonObj.get("aws"), is(instanceOf(JsonObject.class)));
+ JsonObject aws = (JsonObject) jsonObj.get("aws");
+ assertThat(aws.get("xray"), is(instanceOf(JsonObject.class)));
+ JsonObject xray = (JsonObject) aws.get("xray");
+ assertThat(xray.getString("sdk_version"), is(equalTo("1.2.0")));
+ assertThat(xray.getString("sdk"), is(equalTo("X-Ray for Java")));
+ assertThat(jsonObj.get("service"), is(instanceOf(JsonObject.class)));
+ JsonObject service = (JsonObject) jsonObj.get("service");
+ assertThat(service.getString("runtime"), is(equalTo("Java HotSpot(TM) 64-Bit Server VM")));
+ assertThat(service.getString("runtime_version"), is(equalTo("1.8.0_144")));
+ }
+
+ @Test
+ public void testJsonParseWithArray() {
+ JsonStructure json = JsonParser.parse("{"
+ + " \"name\":\"c\","
+ + " \"id\":\"6ada7c7013b2c681\","
+ + " \"start_time\":1.509484895232E9,"
+ + " \"trace_id\":\"1-59f8e935-11c64d09c90803f69534c9af\","
+ + " \"end_time\":1.509484901458E9,"
+ + " \"subsegments\":["
+ + " {"
+ + " \"name\":\"SendingTo_log_test\","
+ + " \"id\":\"545118f5c69e2973\","
+ + " \"start_time\":1.509484895813E9,"
+ + " \"end_time\":1.509484896709E9"
+ + " }"
+ + " ],"
+ + " \"aws\":{"
+ + " \"xray\":{"
+ + " \"sdk_version\":\"1.2.0\","
+ + " \"sdk\":\"X-Ray for Java\""
+ + " }"
+ + " },"
+ + " \"service\":{"
+ + " \"runtime\":\"Java HotSpot(TM) 64-Bit Server VM\","
+ + " \"runtime_version\":\"1.8.0_144\""
+ + " }"
+ + "}\u0000\u0000");
+
+ assertThat(json, is(notNullValue()));
+ JsonObject jsonObj = (JsonObject) json;
+ assertThat(jsonObj.getKeys().size(), is(equalTo(8)));
+ assertThat(jsonObj.getString("name"), is(equalTo("c")));
+ assertThat(jsonObj.getString("id"), is(equalTo("6ada7c7013b2c681")));
+ assertThat(jsonObj.getString("trace_id"), is(equalTo("1-59f8e935-11c64d09c90803f69534c9af")));
+ assertThat(jsonObj.getDouble("start_time"), is(equalTo(1.509484895232E9)));
+ assertThat(jsonObj.getDouble("end_time"), is(equalTo(1.509484901458E9)));
+ assertThat(jsonObj.get("aws"), is(instanceOf(JsonObject.class)));
+ JsonObject aws = (JsonObject) jsonObj.get("aws");
+ assertThat(aws.get("xray"), is(instanceOf(JsonObject.class)));
+ JsonObject xray = (JsonObject) aws.get("xray");
+ assertThat(xray.getString("sdk_version"), is(equalTo("1.2.0")));
+ assertThat(xray.getString("sdk"), is(equalTo("X-Ray for Java")));
+ assertThat(jsonObj.get("service"), is(instanceOf(JsonObject.class)));
+ JsonObject service = (JsonObject) jsonObj.get("service");
+ assertThat(service.getString("runtime"), is(equalTo("Java HotSpot(TM) 64-Bit Server VM")));
+ assertThat(service.getString("runtime_version"), is(equalTo("1.8.0_144")));
+ assertThat(jsonObj.get("subsegments"), is(instanceOf(JsonArray.class)));
+ JsonArray array = (JsonArray) jsonObj.get("subsegments");
+ assertThat(array.size(), is(equalTo(1)));
+ assertThat(array.get(0), is(instanceOf(JsonObject.class)));
+ JsonObject arrItem = (JsonObject) array.get(0);
+ assertThat(arrItem.getKeys().size(), is(equalTo(4)));
+ assertThat(arrItem.getString("name"), is(equalTo("SendingTo_log_test")));
+ assertThat(arrItem.getString("id"), is(equalTo("545118f5c69e2973")));
+ assertThat(arrItem.getDouble("start_time"), is(equalTo(1.509484895813E9)));
+ assertThat(arrItem.getDouble("end_time"), is(equalTo(1.509484896709E9)));
+ }
+}
--
To stop receiving notification emails like this one, please contact
"commits@camel.apache.org" <co...@camel.apache.org>.