Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better support for read/write to/from JsonElement tree
- Loading branch information
Showing
11 changed files
with
171 additions
and
122 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
gson/src/main/java/com/gilecode/yagson/adapters/RawTypeAdapter.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,54 @@ | ||
/* | ||
* Copyright (C) 2016 Andrey Mogilev | ||
* | ||
* 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.gilecode.yagson.adapters; | ||
|
||
import com.gilecode.yagson.ReadContext; | ||
import com.gilecode.yagson.WriteContext; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Special low-level type adapters which does not perform any processing of types or references YaGson metadata, so | ||
* do not use contexts at all. It is supposed that low-level representation of that metadata is processed somewhere | ||
* else. | ||
* <p/> | ||
* For example, a type adapter for {@link com.google.gson.JsonElement} would treats references as simple JSON strings, | ||
* so it is a task of {@link com.google.gson.internal.bind.JsonTreeReader} to convert the references to actual objects. | ||
* Similarly, for writes, it is a task of some external class to convert type/references information into the | ||
* corresponding JSON elements. | ||
* | ||
* @author Andrey Mogilev | ||
*/ | ||
public abstract class RawTypeAdapter<T> extends TypeAdapter<T> { | ||
|
||
abstract public void write(JsonWriter out, T value) throws IOException; | ||
abstract public T read(JsonReader in) throws IOException; | ||
|
||
@Override | ||
public T read(JsonReader in, ReadContext ctx) throws IOException { | ||
// ignore context | ||
return read(in); | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter out, T value, WriteContext ctx) throws IOException { | ||
// ignore context | ||
write(out, value); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,94 @@ | ||
/* | ||
* Copyright (C) 2009 Google Inc. | ||
* Modifications copyright (C) 2016 Andrey Mogilev | ||
* | ||
* 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.google.gson; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
|
||
import com.gilecode.yagson.ReadContext; | ||
import com.google.gson.internal.Streams; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.MalformedJsonException; | ||
|
||
/** | ||
* A parser to parse Json into a parse tree of {@link JsonElement}s | ||
* | ||
* @author Inderjeet Singh | ||
* @author Joel Leitch | ||
* @since 1.3 | ||
*/ | ||
public final class JsonParser { | ||
|
||
/** | ||
* Parses the specified JSON string into a parse tree | ||
* | ||
* @param json JSON text | ||
* @return a parse tree of {@link JsonElement}s corresponding to the specified JSON | ||
* @throws JsonParseException if the specified text is not valid JSON | ||
* @since 1.3 | ||
*/ | ||
public JsonElement parse(String json) throws JsonSyntaxException { | ||
return parse(new StringReader(json)); | ||
} | ||
|
||
/** | ||
* Parses the specified JSON string into a parse tree | ||
* | ||
* @param json JSON text | ||
* @return a parse tree of {@link JsonElement}s corresponding to the specified JSON | ||
* @throws JsonParseException if the specified text is not valid JSON | ||
* @since 1.3 | ||
*/ | ||
public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException { | ||
try { | ||
JsonReader jsonReader = new JsonReader(json); | ||
JsonElement element = parse(jsonReader); | ||
if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) { | ||
throw new JsonSyntaxException("Did not consume the entire document."); | ||
} | ||
return element; | ||
} catch (MalformedJsonException e) { | ||
throw new JsonSyntaxException(e); | ||
} catch (IOException e) { | ||
throw new JsonIOException(e); | ||
} catch (NumberFormatException e) { | ||
throw new JsonSyntaxException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the next value from the JSON stream as a parse tree. | ||
* | ||
* @throws JsonParseException if there is an IOException or if the specified | ||
* text is not valid JSON | ||
* @since 1.6 | ||
*/ | ||
public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { | ||
boolean lenient = json.isLenient(); | ||
json.setLenient(true); | ||
try { | ||
return Streams.parse(json, ReadContext.nullContext()); | ||
} catch (StackOverflowError e) { | ||
throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); | ||
} catch (OutOfMemoryError e) { | ||
throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); | ||
} finally { | ||
json.setLenient(lenient); | ||
} | ||
} | ||
} | ||
/* | ||
* Copyright (C) 2009 Google Inc. | ||
* Modifications copyright (C) 2016 Andrey Mogilev | ||
* | ||
* 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.google.gson; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
|
||
import com.google.gson.internal.Streams; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.MalformedJsonException; | ||
|
||
/** | ||
* A parser to parse Json into a parse tree of {@link JsonElement}s | ||
* | ||
* @author Inderjeet Singh | ||
* @author Joel Leitch | ||
* @since 1.3 | ||
*/ | ||
public final class JsonParser { | ||
|
||
/** | ||
* Parses the specified JSON string into a parse tree | ||
* | ||
* @param json JSON text | ||
* @return a parse tree of {@link JsonElement}s corresponding to the specified JSON | ||
* @throws JsonParseException if the specified text is not valid JSON | ||
* @since 1.3 | ||
*/ | ||
public JsonElement parse(String json) throws JsonSyntaxException { | ||
return parse(new StringReader(json)); | ||
} | ||
|
||
/** | ||
* Parses the specified JSON string into a parse tree | ||
* | ||
* @param json JSON text | ||
* @return a parse tree of {@link JsonElement}s corresponding to the specified JSON | ||
* @throws JsonParseException if the specified text is not valid JSON | ||
* @since 1.3 | ||
*/ | ||
public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException { | ||
try { | ||
JsonReader jsonReader = new JsonReader(json); | ||
JsonElement element = parse(jsonReader); | ||
if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) { | ||
throw new JsonSyntaxException("Did not consume the entire document."); | ||
} | ||
return element; | ||
} catch (MalformedJsonException e) { | ||
throw new JsonSyntaxException(e); | ||
} catch (IOException e) { | ||
throw new JsonIOException(e); | ||
} catch (NumberFormatException e) { | ||
throw new JsonSyntaxException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the next value from the JSON stream as a parse tree. | ||
* | ||
* @throws JsonParseException if there is an IOException or if the specified | ||
* text is not valid JSON | ||
* @since 1.6 | ||
*/ | ||
public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { | ||
boolean lenient = json.isLenient(); | ||
json.setLenient(true); | ||
try { | ||
return Streams.parse(json); | ||
} catch (StackOverflowError e) { | ||
throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); | ||
} catch (OutOfMemoryError e) { | ||
throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); | ||
} finally { | ||
json.setLenient(lenient); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.