/** * Converts {@code value} to a JSON document. Unlike Gson's similar {@link * Gson#toJson(Object) toJson} method, this write is strict. Create a {@link * JsonWriter#setLenient(boolean) lenient} {@code JsonWriter} and call * {@link #write(com.google.gson.stream.JsonWriter, Object)} for lenient * writing. * * @param value the Java object to convert. May be null. * @since 2.2 */ public final String toJson(T value) { StringWriter stringWriter = new StringWriter(); try { toJson(stringWriter, value); } catch (IOException e) { throw new AssertionError(e); // No I/O writing to a StringWriter. } return stringWriter.toString(); }
public QuestDataUpdateMessage(UUID questId, int players, QuestData data) { this.questId = questId; this.data = QuestDataAdapter.QUEST_DATA_ADAPTER.toJson(data); this.players = players; }
@Override public RequestBody convert(T value) throws IOException { String body = adapter.toJson(value); return RequestBody.create(MEDIA_TYPE, body); } }
/** * Converts {@code value} to a JSON document. Unlike Gson's similar {@link * Gson#toJson(Object) toJson} method, this write is strict. Create a {@link * JsonWriter#setLenient(boolean) lenient} {@code JsonWriter} and call * {@link #write(com.google.gson.stream.JsonWriter, Object)} for lenient * writing. * * @param value the Java object to convert. May be null. */ /*public*/ final String toJson(T value) throws IOException { StringWriter stringWriter = new StringWriter(); toJson(stringWriter, value); return stringWriter.toString(); }
/** * Converts {@code value} to a JSON document. Unlike Gson's similar {@link * Gson#toJson(Object) toJson} method, this write is strict. Create a {@link * JsonWriter#setLenient(boolean) lenient} {@code JsonWriter} and call * {@link #write(JsonWriter, Object)} for lenient * writing. * * @param value the Java object to convert. May be null. * @since 2.2 */ public final String toJson(T value) throws IOException { StringWriter stringWriter = new StringWriter(); toJson(stringWriter, value); return stringWriter.toString(); }
/** * Converts {@code value} to a JSON document. Unlike Gson's similar {@link * Gson#toJson(Object) toJson} method, this write is strict. Create a {@link * JsonWriter#setLenient(boolean) lenient} {@code JsonWriter} and call * {@link #write(com.google.gson.stream.JsonWriter, Object)} for lenient * writing. * * @param value the Java object to convert. May be null. * @since 2.2 */ public final String toJson(T value) throws IOException { StringWriter stringWriter = new StringWriter(); toJson(stringWriter, value); return stringWriter.toString(); }
/** * Converts {@code value} to a JSON document. Unlike Gson's similar {@link * Gson#toJson(Object) toJson} method, this write is strict. Create a {@link * JsonWriter#setLenient(boolean) lenient} {@code JsonWriter} and call * {@link #write(com.google.gson.stream.JsonWriter, Object)} for lenient * writing. * * @param value the Java object to convert. May be null. * @since 2.2 */ public final String toJson(T value) { StringWriter stringWriter = new StringWriter(); try { toJson(stringWriter, value); } catch (IOException e) { throw new AssertionError(e); // No I/O writing to a StringWriter. } return stringWriter.toString(); }
/** * Converts {@code value} to a JSON document. Unlike Gson's similar {@link * Gson#toJson(Object) toJson} method, this write is strict. Create a {@link * JsonWriter#setLenient(boolean) lenient} {@code JsonWriter} and call * {@link #write(com.google.gson.stream.JsonWriter, Object)} for lenient * writing. * * @param value the Java object to convert. May be null. * @since 2.2 */ public final String toJson(T value) { StringWriter stringWriter = new StringWriter(); try { toJson(stringWriter, value); } catch (IOException e) { throw new AssertionError(e); // No I/O writing to a StringWriter. } return stringWriter.toString(); }
/** * Converts {@code value} to a JSON document. Unlike Gson's similar {@link * Gson#toJson(Object) toJson} method, this write is strict. Create a {@link * JsonWriter#setLenient(boolean) lenient} {@code JsonWriter} and call * {@link #write(com.google.gson.stream.JsonWriter, Object)} for lenient * writing. * * @param value the Java object to convert. May be null. * @since 2.2 */ public final String toJson(T value) { StringWriter stringWriter = new StringWriter(); try { toJson(stringWriter, value); } catch (IOException e) { throw new AssertionError(e); // No I/O writing to a StringWriter. } return stringWriter.toString(); }
@Override public TeamUpdateMessage build(Team team, Object... data) { return new TeamUpdateMessage(this, TeamAdapter.TEAM_ADAPTER.toJson(team)); } };
@Override public TeamUpdateMessage build(Team team, Object... data) { return new TeamUpdateMessage(this, TeamAdapter.TEAM_ADAPTER.toJson(team)); } },
@Override public TeamUpdateMessage build(Team team, Object... data) { return new TeamUpdateMessage(this, data[0].toString() + TeamAdapter.TEAM_ADAPTER.toJson(team)); } },
@Override public TeamUpdateMessage build(Team team, Object... data) { return new TeamUpdateMessage(this, data[0].toString() + TeamAdapter.TEAM_ADAPTER.toJson((Team) data[1])); } },
@Override public TeamUpdateMessage build(Team team, Object... data) { return new TeamUpdateMessage(this, TeamAdapter.TEAM_ADAPTER.toJson(team)); } },
@Test public void color_state_list() throws IOException { int[][] states = new int[][]{ new int[]{1, 2, 3}, new int[]{4, 5, 6} }; int[] colors = new int[]{7, 8, 9}; Value in = Color.StateList.valueOf(states, colors); String json = adapter.toJson(in); Value out = adapter.fromJson(json); assertThat(1, is(1)); }
public void testRenamedFields() throws IOException { TypeAdapter<RenamedFields> adapter = parameterizedCtorFactory.create(gson, TypeToken.get(RenamedFields.class)); assertEquals(new RenamedFields(0, 1), adapter.fromJson("{\"foo\":0,\"_bar\":1}")); assertEquals(adapter.toJson(new RenamedFields(0, 1)), "{\"foo\":0,\"_bar\":1}"); }
@Test public void primitive() throws IOException { Primitive in = new Primitive("this is a string"); String json = adapter.toJson(in); Value out = adapter.fromJson(json); assertThat(in, is(out)); }
public void testRenamedFields() throws IOException { TypeAdapter<RenamedFields> adapter = parameterizedCtorFactory.create(gson, TypeToken.get(RenamedFields.class)); assertEquals(new RenamedFields(0, 1), adapter.fromJson("{\"foo\":0,\"_bar\":1}")); assertEquals(adapter.toJson(new RenamedFields(0, 1)), "{\"foo\":0,\"_bar\":1}"); }
/** * Verifies that the type adapter for a class is correct. It does this by manufacturing an * instance of the class, writing it to JSON, and then reading that object back out of JSON * and comparing the two instances. * * @param clazz the {@link Class} to use to get the {@link TypeAdapter}. */ public static <T> void verifyTypeAdapterCorrectness(@NotNull Class<T> clazz) { final PodamFactory factory = new PodamFactoryImpl(); factory.setClassStrategy(new HungarianNotationClassInfoStrategy()); final T object = factory.manufacturePojo(clazz); final TypeAdapter<T> typeAdapter = getTypeAdapter(clazz); T newObject = null; if (typeAdapter != null) { final String json = typeAdapter.toJson(object); try { newObject = typeAdapter.fromJson(json); } catch (IOException ignored) {} } assertEquals(object, newObject); }
@Test public void object() throws IOException { ObjectValue in = new ObjectValue(); in.add("a", new Primitive(1)); in.add("b", new Primitive(2)); //in.add("c", new Array(new Value[]{new Primitive(3), Color.valueOf("#545454")})); ObjectValue nested = new ObjectValue(); //nested.add("d", AttributeResource.valueOf(123)); //nested.add("e", Binding.valueOf("@{a.b.c}", proteus.functions)); //nested.add("f", Binding.valueOf("@{x.y.z}${number()}", proteus.functions)); //nested.add("g", Binding.valueOf("date is @{a.b.c}${date('E, MM yyyy')}", proteus.functions)); in.add("z", nested); String json = adapter.toJson(in); Value out = adapter.fromJson(json); assertThat(1, is(1)); }