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

net.bramp.commons.lang3.math.gson.FractionAdapter Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
package net.bramp.commons.lang3.math.gson;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import org.apache.commons.lang3.math.Fraction;

import java.io.IOException;

/**
 * GSON TypeAdapter for Apache Commons Math Fraction Object
 *
 * @author bramp
 *
 */
public class FractionAdapter extends TypeAdapter {

  public Fraction read(JsonReader reader) throws IOException {
    JsonToken next = reader.peek();

    if (next == JsonToken.NULL) {
      reader.nextNull();
      return null;
    }

    if (next == JsonToken.NUMBER) {
      return Fraction.getFraction(reader.nextDouble());
    }

    String fraction = reader.nextString();

    // Ambiguous as to what 0/0 is, but FFmpeg seems to think it's zero
    if (fraction.equals("0/0"))
      return Fraction.ZERO;

    return Fraction.getFraction(fraction);
  }

  public void write(JsonWriter writer, Fraction value) throws IOException {
    if (value == null) {
      writer.nullValue();
      return;
    }
    writer.value(value.toProperString());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy