All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prerna.util.gson.NumberAdapter Maven / Gradle / Ivy

The newest version!
package prerna.util.gson;

import java.io.IOException;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

public class NumberAdapter extends TypeAdapter {
	
	/**
	 * Generation of new NumberAdaptor to not send NaN/Infinity to the FE
	 * since they are invalid JSON values
	 */

	@Override 
	public Double read(JsonReader in) throws IOException {
		if (in.peek() == JsonToken.NULL) {
			in.nextNull();
			return null;
		}
		return in.nextDouble();
	}

	@Override 
	public void write(JsonWriter out, Double value) throws IOException {
		if (value == null) {
			out.nullValue();
			return;
		}
		double doubleValue = value.doubleValue();
		if(Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
			out.nullValue();
		} else {
			out.value(value);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy