Skip to content

Commit

Permalink
There's still a lot of work to get JSON diagnostic right.
Browse files Browse the repository at this point in the history
Introduced getLocationInfo():String[] on Gson-Jackson bridge and let
consumer to how to format them with regards to commas, newlines and
tabs.
  • Loading branch information
augustotravillio committed Apr 24, 2015
1 parent a0a17a1 commit b9b1b8b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
Expand Up @@ -15,6 +15,7 @@
*/
package org.immutables.gson.stream;

import javax.ws.rs.core.Response.Status;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
Expand Down Expand Up @@ -144,11 +145,10 @@ public Object readFrom(
try {
return streamer.read(gson, genericType, entityStream);
} catch (IOException ex) {
// Experimental
if (ex.getCause() instanceof RuntimeException) {
String json = gson.toJson(new Error(ex.getCause().getMessage()));
throw new WebApplicationException(
Response.status(400)
Response.status(Status.BAD_REQUEST)
.type(mediaType)
.entity(json)
.build());
Expand All @@ -160,8 +160,11 @@ public Object readFrom(
static class Error {
final String error;

Error(String error) {
// final String[] location;

Error(String error) {// , String[] location) {
this.error = error;
// this.location = location;
}
}

Expand Down
8 changes: 5 additions & 3 deletions gson/src/org/immutables/gson/stream/JsonGeneratorWriter.java
Expand Up @@ -147,9 +147,11 @@ public void close() throws IOException {

@Override
public String toString() {
String nt = "\n\t";
return getClass().getSimpleName() + "()"
+ nt + "path " + getPath();
return getClass().getSimpleName() + "(" + generator + ")";
}

public String[] getLocationInfo() {
return new String[] {"path " + getPath()};
}

/**
Expand Down
47 changes: 35 additions & 12 deletions gson/src/org/immutables/gson/stream/JsonParserReader.java
Expand Up @@ -15,11 +15,6 @@
*/
package org.immutables.gson.stream;

import com.google.common.base.Throwables;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonStreamContext;
Expand All @@ -28,6 +23,8 @@
import com.google.gson.stream.JsonToken;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
Expand Down Expand Up @@ -267,11 +264,15 @@ public String getPath() {

@Override
public String toString() {
String nt = "\n\t";
return getClass().getSimpleName() + "()"
+ nt + "path " + getPath()
+ nt + "token " + getTokenString()
+ nt + "at " + getLocationString();
return getClass().getSimpleName() + "(" + parser + ")";
}

public String[] getLocationInfo() {
return new String[] {
"path " + getPath(),
"token " + getTokenString(),
"at " + getLocationString()
};
}

private String getTokenString() {
Expand All @@ -287,7 +288,7 @@ private String getTokenString() {

private String getLocationString() {
JsonLocation l = parser.getCurrentLocation();
List<String> parts = new ArrayList<>();
List<String> parts = new ArrayList<>(4);
parts.add("line: " + l.getLineNr());
parts.add("column: " + l.getColumnNr());
if (l.getByteOffset() >= 0) {
Expand All @@ -302,11 +303,33 @@ static String toJsonPath(JsonStreamContext context) {
if (c.inArray()) {
builder.insert(0, "[" + c.getCurrentIndex() + "]");
} else if (c.inObject()) {
builder.insert(0, "." + c.getCurrentName());
String name = c.getCurrentName();
if (isAsciiIdentifierPath(name)) {
builder.insert(0, "." + name);
} else {
builder.insert(0, "['" + name + "']");
}
} else if (c.inRoot()) {
builder.insert(0, "$");
}
}
return builder.toString();
}

private static boolean isAsciiIdentifierPath(String name) {
char[] cs = name.toCharArray();
if (cs.length == 0) {
return false;
}
for (int i = 0; i < cs.length; i++) {
char c = cs[i];
if ((c != '_')
&& (c < 'A' || c > 'Z')
&& (c < 'a' || c > 'z')
&& (c < '0' || c > '9' || i == 0)) {
return false;
}
}
return true;
}
}

0 comments on commit b9b1b8b

Please sign in to comment.