com.yahoo.elide.test.graphql.VariableFieldSerializer Maven / Gradle / Ivy
/*
* Copyright 2019, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.test.graphql;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
/**
* A Jackson serializer for String entity field.
*
* {@link VariableFieldSerializer} serializes a String field differently when value can both represents a concrete value
* or a GraphQL variable. On concrete value, it outputs the original value unmodified and quoted; on variable value, it
* does not quote the original value.
*
* For example, given the following entity:
*
* {@code
* public class Book {
*
* {@literal @}JsonSerialize(using = VariableFieldSerializer.class, as = String.class)
* private String title;
* }
* }
*
* A {@code Book(title="Java Concurrency in Practice")} serializes to
*
* {"title": "Java Concurrency in Practice"}
*
* However a {@code Book(title="$titlePassedByClient")} serializes to
*
* {"title": $titlePassedByClient}
*
* Note in the 1st serialization {@code title} value is quoted while the 2nd serialization it is not.
*
* To serialize a String entity field in such a way, add the following annotation to the field, as shown above:
*
* {@code
* {@literal @}JsonSerialize(using = VariableFieldSerializer.class, as = String.class)
* }
*
*
* @see Variables
*/
public class VariableFieldSerializer extends JsonSerializer {
private static final String VARIABLE_SIGN = "$";
private static final String ENUM_SIGN = "#";
@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value.startsWith(VARIABLE_SIGN)) {
// this is a variable
gen.writeRawValue(value);
} else if (value.startsWith(ENUM_SIGN)) {
// this is an enum
gen.writeRawValue(value.substring(1));
} else {
gen.writeString(value);
}
}
}