Skip to content

Commit

Permalink
Common: Move Json parser from Runtime to Common
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Forslund committed Jun 3, 2016
1 parent 7346bae commit aba1ea0
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 40 deletions.
60 changes: 60 additions & 0 deletions common/json/pom.xml
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.speedment.common</groupId>
<artifactId>common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>json</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>bundle</packaging>

<name>Speedment - Common - JSON</name>
<description>
A lightweight JSON serializer and deserializer made with the ambition to
reduce complexity for non-performance critical parsing situations.
</description>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.1</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Vendor>Speedment, Inc.</Bundle-Vendor>
<Bundle-Description>
A lightweight JSON serializer and deserializer made
with the ambition to reduce complexity for
non-performance critical parsing situations.
</Bundle-Description>
<Import-Package>
org.osgi.framework,
*;resolution:=optional
</Import-Package>
<Export-Package>com.speedment.common.json</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -14,10 +14,10 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.runtime.internal.util.json;
package com.speedment.common.json;

import com.speedment.runtime.exception.SpeedmentException;
import static com.speedment.runtime.util.StaticClassUtil.instanceNotAllowed;
import com.speedment.common.json.internal.JsonDeserializer;
import com.speedment.common.json.internal.JsonSerializer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -31,7 +31,7 @@
* JSON is parsed using the {@code RFC 7159} specification.
*
* @author Emil Forslund
* @since 2.4.0
* @since 1.0.0
*/
public final class Json {

Expand Down Expand Up @@ -63,7 +63,7 @@ public static String toJson(Object object) throws IllegalArgumentException {
toJson(object, out);
return new String(out.toByteArray(), StandardCharsets.UTF_8);
} catch (final IOException ex) {
throw new SpeedmentException(
throw new RuntimeException(
"Error in internal toString()-stream.", ex
);
}
Expand Down Expand Up @@ -149,7 +149,5 @@ public static Object fromJson(InputStream in) throws IOException, JsonSyntaxExce
/**
* Utility classes should never be instantiated.
*/
private Json() {
instanceNotAllowed(getClass());
}
private Json() {}
}
Expand Up @@ -14,15 +14,15 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.runtime.internal.util.json;
package com.speedment.common.json;

import java.util.concurrent.atomic.AtomicLong;

/**
* A special form of exception that is thrown when parsing of a json file fails.
*
* @author Emil Forslund
* @since 2.4.0
* @since 1.0.0
*/
public final class JsonSyntaxException extends RuntimeException {

Expand Down
Expand Up @@ -14,8 +14,9 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.runtime.internal.util.json;
package com.speedment.common.json.internal;

import com.speedment.common.json.JsonSyntaxException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -38,9 +39,9 @@
* have errors.
*
* @author Emil Forslund
* @since 2.4.0
* @since 1.0.0
*/
final class JsonDeserializer implements AutoCloseable {
public final class JsonDeserializer implements AutoCloseable {

private final static String ENCODING = "UTF-8";
private final static int TAB_SIZE = 4;
Expand All @@ -51,7 +52,7 @@ final class JsonDeserializer implements AutoCloseable {

private int character;

JsonDeserializer(InputStream in) throws UnsupportedEncodingException {
public JsonDeserializer(InputStream in) throws UnsupportedEncodingException {
reader = new InputStreamReader(in, ENCODING);
row = new AtomicLong(0);
col = new AtomicLong(0);
Expand All @@ -63,7 +64,7 @@ private enum CloseMethod {
NOT_DECIDED
}

Object get() throws IOException {
public Object get() throws IOException {
switch (nextNonBlankspace()) {
case 0x7B : // { (begin parsing object)
return parseObject();
Expand Down
Expand Up @@ -14,7 +14,7 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.runtime.internal.util.json;
package com.speedment.common.json.internal;

import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -26,22 +26,22 @@
* An internal class that can serialize java objects into JSON code.
*
* @author Emil Forslund
* @since 2.4.0
* @since 1.0.0
*/
final class JsonSerializer {
public final class JsonSerializer {

private final OutputStream out;
private final boolean pretty;
private final int tabSize;
private int level = 0;

JsonSerializer(OutputStream out, boolean pretty) {
public JsonSerializer(OutputStream out, boolean pretty) {
this.out = requireNonNull(out);
this.pretty = pretty;
this.tabSize = pretty ? PRETTY_TAB_SIZE : 0;
}

void print(Object unknown) throws IOException {
public void print(Object unknown) throws IOException {
if (unknown == null) {
printNull();
} else {
Expand Down
@@ -1,20 +1,4 @@
/**
*
* Copyright (c) 2006-2016, Speedment, Inc. All Rights Reserved.
*
* 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.
*/
package com.speedment.runtime.internal.util.json;
package com.speedment.common.json;

import java.util.Arrays;
import java.util.List;
Expand All @@ -24,7 +8,7 @@

/**
*
* @author Emil
* @author Emil Forslund
*/
public class JsonTest {

Expand Down Expand Up @@ -117,4 +101,4 @@ public void testParse_ArrayOfObjects() {
assertEquals("Check one: ", 2L, list.get(1).get("two"));
assertEquals("Check one: ", 3L, list.get(2).get("three"));
}
}
}
2 changes: 2 additions & 0 deletions common/pom.xml
Expand Up @@ -38,8 +38,10 @@
</description>

<modules>
<module>json</module>
<module>logger</module>
<module>mapstream</module>
<module>codegen</module>
<module>codegenxml</module>
</modules>
</project>
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -64,6 +64,7 @@
<speedment.version>2.4.0-SNAPSHOT</speedment.version>
<mapstream.version>2.3.3-SNAPSHOT</mapstream.version>
<logger.version>1.0.1-SNAPSHOT</logger.version>
<json.version>1.0.0-SNAPSHOT</json.version>
</properties>

<organization>
Expand Down
6 changes: 6 additions & 0 deletions runtime/pom.xml
Expand Up @@ -86,6 +86,12 @@
<version>${logger.version}</version>
</dependency>

<dependency>
<groupId>com.speedment.common</groupId>
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Expand Up @@ -16,11 +16,11 @@
*/
package com.speedment.runtime.internal.util.document;

import com.speedment.common.json.Json;
import com.speedment.runtime.annotation.Api;
import com.speedment.runtime.config.Project;
import com.speedment.runtime.exception.SpeedmentException;
import com.speedment.runtime.internal.config.ProjectImpl;
import com.speedment.runtime.internal.util.json.Json;
import static com.speedment.runtime.util.StaticClassUtil.instanceNotAllowed;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down

0 comments on commit aba1ea0

Please sign in to comment.