|
Java example source code file (JsonAdapterSerializerDeserializerTest.java)
The JsonAdapterSerializerDeserializerTest.java Java example source code
/*
* Copyright (C) 2016 Google Inc.
*
* 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.functional;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.annotations.JsonAdapter;
import junit.framework.TestCase;
/**
* Functional tests for the {@link JsonAdapter} annotation on fields where the value is of
* type {@link JsonSerializer} or {@link JsonDeserializer}.
*/
public final class JsonAdapterSerializerDeserializerTest extends TestCase {
public void testJsonSerializerDeserializerBasedJsonAdapterOnFields() {
Gson gson = new Gson();
String json = gson.toJson(new Computer(new User("Inderjeet Singh"), null, new User("Jesse Wilson")));
assertEquals("{\"user1\":\"UserSerializer\",\"user3\":\"UserSerializerDeserializer\"}", json);
Computer computer = gson.fromJson("{'user2':'Jesse Wilson','user3':'Jake Wharton'}", Computer.class);
assertEquals("UserSerializer", computer.user2.name);
assertEquals("UserSerializerDeserializer", computer.user3.name);
}
private static final class Computer {
@JsonAdapter(UserSerializer.class) final User user1;
@JsonAdapter(UserDeserializer.class) final User user2;
@JsonAdapter(UserSerializerDeserializer.class) final User user3;
Computer(User user1, User user2, User user3) {
this.user1 = user1;
this.user2 = user2;
this.user3 = user3;
}
}
private static final class User {
public final String name;
private User(String name) {
this.name = name;
}
}
private static final class UserSerializer implements JsonSerializer<User> {
@Override
public JsonElement serialize(User src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive("UserSerializer");
}
}
private static final class UserDeserializer implements JsonDeserializer<User> {
@Override
public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new User("UserSerializer");
}
}
private static final class UserSerializerDeserializer implements JsonSerializer<User>, JsonDeserializer
Other Java examples (source code examples)Here is a short list of links related to this Java JsonAdapterSerializerDeserializerTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.