-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#126: Parse Object into a JsonElement
- Loading branch information
1 parent
b4659be
commit 74e8804
Showing
16 changed files
with
574 additions
and
9 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
63 changes: 63 additions & 0 deletions
63
...jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/json/adapters/JsonElementTypeAdapter.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,63 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2018 TypeFox GmbH (http://www.typefox.io) and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*******************************************************************************/ | ||
package org.eclipse.lsp4j.jsonrpc.json.adapters; | ||
|
||
import java.io.IOException; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.TypeAdapterFactory; | ||
import com.google.gson.internal.bind.TypeAdapters; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
/** | ||
* A type adapter that reads every input into a tree of {@link JsonElement}s. | ||
*/ | ||
public class JsonElementTypeAdapter extends TypeAdapter<Object> { | ||
|
||
/** | ||
* This factory should not be registered with a GsonBuilder because it always matches. | ||
* Use it as argument to a {@link com.google.gson.annotations.JsonAdapter} annotation like this: | ||
* {@code @JsonAdapter(JsonElementTypeAdapter.Factory.class)} | ||
*/ | ||
public static class Factory implements TypeAdapterFactory { | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
return (TypeAdapter<T>) new JsonElementTypeAdapter(gson); | ||
} | ||
|
||
} | ||
|
||
private final Gson gson; | ||
|
||
public JsonElementTypeAdapter(Gson gson) { | ||
this.gson = gson; | ||
} | ||
|
||
@Override | ||
public JsonElement read(JsonReader in) throws IOException { | ||
return TypeAdapters.JSON_ELEMENT.read(in); | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter out, Object value) throws IOException { | ||
if (value == null) { | ||
out.nullValue(); | ||
} else if (value instanceof JsonElement) { | ||
TypeAdapters.JSON_ELEMENT.write(out, (JsonElement) value); | ||
} else { | ||
gson.toJson(value, value.getClass(), out);; | ||
} | ||
} | ||
|
||
} |
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
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.