How to detect if a number is double or int while parsing

2,103 views
Skip to first unread message

uman

unread,
Apr 25, 2011, 5:14:43 AM4/25/11
to google-gson
Hey.

I'm using JsonParser to get a tree representation which I then walk
through. I need to do this for my specific application needs because I
can't know what objects are coming (if you have suggestions for this
scenario, please tell).

My problem is when I reach a primitive that is a number. In the JSON
string it will be 3 say, but I don't get how I can write my code so
that I avoid e.g. converting it to a double because getAsDouble will
simply return 3.0. But it is important for my application that it be 3
and not 3.0 if the string indeed was 3. Similarly, if the JSON
contains 3.7, I would want to be able to see that this is not an
integer. Currently, if I call getAsInt I just get 3.

Is there some easy solution to this problem?

Joel

unread,
Apr 25, 2011, 3:51:23 PM4/25/11
to google-gson
Are you able to use "BigDecimal" instead?

ACNT

unread,
Jul 30, 2012, 8:01:13 AM7/30/12
to googl...@googlegroups.com
HI, uman 
I meet this problem too,have you already made it? if you made it ,can you give me some advice ?

在 2011年4月25日星期一UTC+8下午5时14分43秒,uman写道:

Brandon Mintern

unread,
Jul 30, 2012, 6:50:23 PM7/30/12
to googl...@googlegroups.com
Gson has a limitation in that you cannot register a TypeAdapter for
Number.class. You can, however, extend Number with your own class and
write a TypeAdapter for that. Here's an example:

DoubleOrInt.java
===============
import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Arrays;

public class DoubleOrInt extends Number {
private Number number;

public DoubleOrInt(Number n) {
number = n;
}

@Override
public double doubleValue() {
return number.doubleValue();
}

@Override
public float floatValue() {
return number.floatValue();
}

@Override
public int intValue() {
return number.intValue();
}

@Override
public long longValue() {
return number.longValue();
}

public Class<? extends Number> getNumberClass() {
return number.getClass();
}

@Override
public String toString() {
return number.toString();
}

public static class Adapter extends TypeAdapter<DoubleOrInt> {
@Override
public DoubleOrInt read(JsonReader jReader) throws IOException {
String json = jReader.nextString();
Number n;
if (json.indexOf('.') < 0) {
n = Integer.valueOf(json);
} else {
n = Double.valueOf(json);
}
return new DoubleOrInt(n);
}

@Override
public void write(JsonWriter out, DoubleOrInt value) throws
IOException {
out.value(value);
}
}

public static void main(String[] args) {
Gson gson = new
GsonBuilder().registerTypeAdapter(DoubleOrInt.class, new
Adapter()).create();
for (String json: Arrays.asList("42", "3.14")) {
DoubleOrInt n = gson.fromJson(json, DoubleOrInt.class);
System.out.println(n.getNumberClass().getName() + " with
value " + n.toString());
}
}
}
===============
java -cp .:gson-2.2.2.jar DoubleOrInt
java.lang.Integer with value 42
java.lang.Double with value 3.14
> --
> You received this message because you are subscribed to the Google Groups
> "google-gson" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-gson/-/ptVQJxTEpe0J.
> To post to this group, send email to googl...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-gson...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.
Reply all
Reply to author
Forward
0 new messages