Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added smart data parsing tool that detects JSON, XML or YAML data for…
…mat.
- Loading branch information
1 parent
9ca85dc
commit 74b336e
Showing
23 changed files
with
255 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../main/java/org/rapidoid/jackson/JSON.java → ...src/main/java/org/rapidoid/data/JSON.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.rapidoid.jackson; | ||
package org.rapidoid.data; | ||
|
||
import java.io.OutputStream; | ||
import java.util.Map; | ||
|
138 changes: 138 additions & 0 deletions
138
rapidoid-jackson/src/main/java/org/rapidoid/data/Parse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package org.rapidoid.data; | ||
|
||
/* | ||
* #%L | ||
* rapidoid-jackson | ||
* %% | ||
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
|
||
import org.rapidoid.util.U; | ||
|
||
/** | ||
* @author Nikolche Mihajlovski | ||
* @since 4.4.0 | ||
*/ | ||
public class Parse { | ||
|
||
enum DataFormat { | ||
JSON, XML, YAML | ||
}; | ||
|
||
private static final DataFormat[] XML_FIRST = { DataFormat.XML, DataFormat.JSON, DataFormat.YAML }; | ||
|
||
private static final DataFormat[] JSON_FIRST = { DataFormat.JSON, DataFormat.XML, DataFormat.YAML }; | ||
|
||
private static final DataFormat[] YAML_FIRST = { DataFormat.YAML, DataFormat.JSON, DataFormat.XML }; | ||
|
||
public static <T> T data(String data, Class<T> targetType) { | ||
return data(data.getBytes(), targetType); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T> T data(byte[] data, Class<T> targetType) { | ||
if (data == null) { | ||
return null; | ||
} | ||
|
||
// don't parse if the target type is byte[] | ||
if (targetType == byte[].class) { | ||
return (T) data; | ||
} | ||
|
||
DataFormat[] formatsOrder = detectDataFormat(data); | ||
|
||
return tryToParseData(data, formatsOrder, targetType); | ||
} | ||
|
||
private static <T> T tryToParseData(byte[] data, DataFormat[] formatsOrder, Class<T> targetType) { | ||
|
||
Exception firstException = null; | ||
|
||
for (DataFormat dataFormat : formatsOrder) { | ||
switch (dataFormat) { | ||
case JSON: | ||
try { | ||
return JSON.parse(data, targetType); | ||
} catch (Exception e) { | ||
if (firstException == null) { | ||
firstException = e; | ||
} | ||
} | ||
break; | ||
|
||
case XML: | ||
try { | ||
return XML.parse(data, targetType); | ||
} catch (Exception e) { | ||
if (firstException == null) { | ||
firstException = e; | ||
} | ||
} | ||
break; | ||
|
||
case YAML: | ||
try { | ||
return YAML.parse(data, targetType); | ||
} catch (Exception e) { | ||
if (firstException == null) { | ||
firstException = e; | ||
} | ||
} | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
|
||
throw U.rte("Not a valid JSON, XML nor YAML format!", firstException); | ||
} | ||
|
||
static DataFormat[] detectDataFormat(byte[] data) { | ||
if (data.length == 0) { | ||
return YAML_FIRST; | ||
} | ||
|
||
byte start = findFirstNonWhitespaceByte(data); | ||
|
||
if (start == '<') { | ||
return XML_FIRST; | ||
} else if (start == ' ' || (start == '-' && dataStartsWithYAMLMark(data))) { | ||
return YAML_FIRST; | ||
} else { | ||
return JSON_FIRST; | ||
} | ||
} | ||
|
||
private static byte findFirstNonWhitespaceByte(byte[] data) { | ||
int i = 0; | ||
|
||
while (Character.isWhitespace(data[i])) { | ||
i++; | ||
if (i >= data.length) { | ||
return ' '; | ||
} | ||
} | ||
|
||
return data[i]; | ||
} | ||
|
||
private static boolean dataStartsWithYAMLMark(byte[] data) { | ||
return data.length >= 3 && data[0] == '-' && data[1] == '-' && data[2] == '-'; | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...n/src/main/java/org/rapidoid/xml/XML.java → .../src/main/java/org/rapidoid/data/XML.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.rapidoid.xml; | ||
package org.rapidoid.data; | ||
|
||
/* | ||
* #%L | ||
|
2 changes: 1 addition & 1 deletion
2
.../main/java/org/rapidoid/jackson/YAML.java → ...src/main/java/org/rapidoid/data/YAML.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.rapidoid.jackson; | ||
package org.rapidoid.data; | ||
|
||
import java.io.OutputStream; | ||
import java.util.Map; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
rapidoid-jackson/src/test/java/org/rapidoid/data/ParseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.rapidoid.data; | ||
|
||
/* | ||
* #%L | ||
* rapidoid-jackson | ||
* %% | ||
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
|
||
import org.junit.Test; | ||
import org.rapidoid.data.Parse.DataFormat; | ||
import org.rapidoid.test.TestCommons; | ||
|
||
/** | ||
* @author Nikolche Mihajlovski | ||
* @since 4.4.0 | ||
*/ | ||
public class ParseTest extends TestCommons { | ||
|
||
@Test | ||
public void testXMLParse() { | ||
String xml = XML.stringify(new Person("abc", 123)); | ||
System.out.println(xml); | ||
|
||
Person p = Parse.data(xml, Person.class); | ||
|
||
eq(p.getName(), "abc"); | ||
eq(p.getAge(), 123); | ||
} | ||
|
||
@Test | ||
public void testJSONParse() { | ||
String json = JSON.stringify(new Person("abc", 123)); | ||
System.out.println(json); | ||
|
||
Person p = Parse.data(json, Person.class); | ||
|
||
eq(p.getName(), "abc"); | ||
eq(p.getAge(), 123); | ||
|
||
isNull(Parse.data("null", Person.class)); | ||
eq(Parse.data("3", Integer.class).intValue(), 3); | ||
isTrue(Parse.data("true", Boolean.class).booleanValue()); | ||
isFalse(Parse.data("false", Boolean.class).booleanValue()); | ||
eq(Parse.data("\"123\"", String.class), "123"); | ||
} | ||
|
||
@Test | ||
public void testYAMLParse() { | ||
String yaml = YAML.stringify(new Person("abc", 123)); | ||
System.out.println(yaml); | ||
|
||
Person p = Parse.data(yaml, Person.class); | ||
|
||
eq(p.getName(), "abc"); | ||
eq(p.getAge(), 123); | ||
} | ||
|
||
@Test | ||
public void testDataFormatAutoDetect() { | ||
eq(Parse.detectDataFormat("---\n".getBytes())[0], DataFormat.YAML); | ||
eq(Parse.detectDataFormat("".getBytes())[0], DataFormat.YAML); | ||
eq(Parse.detectDataFormat("<abc>".getBytes())[0], DataFormat.XML); | ||
eq(Parse.detectDataFormat("-1".getBytes())[0], DataFormat.JSON); | ||
eq(Parse.detectDataFormat("-12345".getBytes())[0], DataFormat.JSON); | ||
eq(Parse.detectDataFormat("null".getBytes())[0], DataFormat.JSON); | ||
eq(Parse.detectDataFormat("\"fff\"".getBytes())[0], DataFormat.JSON); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...rc/test/java/org/rapidoid/xml/Person.java → ...c/test/java/org/rapidoid/data/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.rapidoid.xml; | ||
package org.rapidoid.data; | ||
|
||
/* | ||
* #%L | ||
|
Oops, something went wrong.