Se sto capendo correttamente, vuoi normalizzare o massaggiare il valore sul JSONReader in arrivo da un int a qualcos'altro, come un booleano.
In tal caso, probabilmente si desidera creare una classe adattatore che estenda TypeAdapter <YourFieldNameType> e sovrascriva read(). Si ottiene il valore da nextInt() e quindi si restituisce il valore booleano appropriato in base al suo valore. Potrebbe essere necessario verificare i valori nulli, a seconda della configurazione del parser.
Se è necessario, è possibile sovrascrivere write() nella stessa classe dell'adattatore per forzare i booleani nel codice client in numeri interi per JsonWriter.
[EDIT]
Per riferimento, ecco un esempio del mio TypeAdapter, che converte un "Comandi" classe enum a/da un numero intero.
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;
/**
* Adapter for Command handling.
*
* We write out the CommandName as an Integer, and read it in as a Commands constant.
*
* This satisfies the requirement that the CommandName by represented by JSON as an int, but allows
* us to deserialize it to a Commands object on read.
*
* @author jdv
* @see com.company.product.Command#commandName CommandName
*/
public class CommandsAdapter extends TypeAdapter<Commands> {
private static final Logger logger = Logger.getLogger(ClientSocket.class.getPackage().getName());
/*
* (non-Javadoc) Deserialize the JSON "CommandName" integer into the corresponding Commands
* constant object.
*
* @see com.google.gson.TypeAdapter#read(com.google.gson.stream.JsonReader)
*/
@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);
}
// Let's not risk using a bad array index. Not every command is expected
// by the WebSocket handlers, but we should do our best to construct
// a valid Commands object.
if (command < NO_OP.getValue() || command > LAST_COMMAND.getValue()) {
throw new IOException(new IllegalArgumentException(
"Unexpected value encountered for Commands constant: " + command));
} else {
return Commands.values()[command];
}
}
/*
* (non-Javadoc) Serialize Commands object constants as their Integer values.
*
* @see com.google.gson.TypeAdapter#write(com.google.gson.stream.JsonWriter, java.lang.Object)
*/
@Override
public void write(JsonWriter out, Commands value) throws IOException {
out.value(value.getValue());
}
}
adatta questa sostanza op entrata e in uscita sul "CommandName" param serializzato rappresentata localmente come un'enumerazione "Comandi" e come numero intero da remoto. Tutto ciò che ha a che fare con questo comando enum viene filtrato attraverso questa classe dell'adattatore che sovrascrive read() e write(). Alla fine questo porta un peer WebSocket, ma niente di tutto questo è importante ai fini di questa discussione. HTH.
Grazie! Se ho capito bene, ogni volta che provo a deserializzare con questo set 'BooleanTypeAdapter', qualsiasi' boolean' sarà trattato in questo modo? – Eliezer
@Maheshwar Ligade Quindi, per quanto riguarda la domanda ** campi specifici ** parte? Questa soluzione non funziona per le risposte contengono il valore intero normale come 'quantity: 0' questa chiave non può essere rappresentata come' Boolean' –