If I understand correctly, you want to normalize or massage the value of the incoming JsonReader from int to another, for example, a logical one.
If so, you probably want to create an adapter class that extends TypeAdapter <YourFieldNameType> and overrides read (). You get the value from nextInt (), and then return the corresponding boolean based on its value. You may need to check for null values depending on the analyzer configuration.
If you need to do this, you can override write () in the same adapter class to force boolean in your client code into integers for JsonWriter.
[EDIT]
For reference, here is an example of my TypeAdapter, which forces the "Commands" enumeration class to / from an integer.
package com.company.product.json; import static com.company.product.Commands.*; import java.io.IOException; import java.util.logging.Logger; import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import com.company.product.Commands; import com.company.product.client.ClientSocket; public class CommandsAdapter extends TypeAdapter<Commands> { private static final Logger logger = Logger.getLogger(ClientSocket.class.getPackage().getName()); @Override public Commands read(JsonReader in) throws IOException { final int command; try { command = in.nextInt(); } catch (IllegalStateException e) { logger.severe("Unable to read incoming JSON stream: " + e.getMessage()); throw new IOException(e); } catch (NumberFormatException e) { logger .severe("Unable to read and convert CommandName Integer from the incoming JSON stream: " + e.getMessage()); throw new IOException(e); }
This essentially adapts the incoming and outgoing operators in the serialized paragraph “CommandName”, which is locally represented as the “Commands” enum and as a whole remotely. Everything related to this enum enumeration is filtered through this adapter class, which overrides read () and write (). In the end, this leads to him being a WebSocket partner, but none of this matters to this discussion. NTN.
source share