Is JsonElement integer or float?
Is it possible to determine whether a GSON JsonElement
instance is an integer or is it a float?
I'm able to determine whether it's a number:
JsonElement value = ...
boolean isNumber = value.getAsJsonPrimitive().isNumber();
But how to determine if it's an integer or a float, so I can subsequently use the correct conversion method? Either
float f = value.getAsJsonPrimitive().getAsFloat();
or
int i = value.getAsJsonPrimitive().getAsInt();
Edit: The other question may answer why this may be not implemented in GSON, but this question definitely isn't its duplicate.
java gson
add a comment |
Is it possible to determine whether a GSON JsonElement
instance is an integer or is it a float?
I'm able to determine whether it's a number:
JsonElement value = ...
boolean isNumber = value.getAsJsonPrimitive().isNumber();
But how to determine if it's an integer or a float, so I can subsequently use the correct conversion method? Either
float f = value.getAsJsonPrimitive().getAsFloat();
or
int i = value.getAsJsonPrimitive().getAsInt();
Edit: The other question may answer why this may be not implemented in GSON, but this question definitely isn't its duplicate.
java gson
2
Possible duplicate of How to prevent Gson from expressing integers as floats
– lucidbrot
Nov 13 '18 at 15:03
@lucidbrot I don't see how this is a duplicate.
– yper
Nov 13 '18 at 15:09
quoting: "Since JSON doesn't distinguish between integer and floating point fields Gson has to default to Float/Double for numeric fields.". Sounds to me like exactly what you're asking about
– lucidbrot
Nov 13 '18 at 15:15
@lucidbrot I see this answer as being orthogonal to my question. Whatever the way Gson defaults numbers, there could be a way of it answering whether the number essentially "contains a dot" or not.
– yper
Nov 13 '18 at 16:29
Okay, then great you've clarified that it's not a dupe
– lucidbrot
Nov 13 '18 at 16:33
add a comment |
Is it possible to determine whether a GSON JsonElement
instance is an integer or is it a float?
I'm able to determine whether it's a number:
JsonElement value = ...
boolean isNumber = value.getAsJsonPrimitive().isNumber();
But how to determine if it's an integer or a float, so I can subsequently use the correct conversion method? Either
float f = value.getAsJsonPrimitive().getAsFloat();
or
int i = value.getAsJsonPrimitive().getAsInt();
Edit: The other question may answer why this may be not implemented in GSON, but this question definitely isn't its duplicate.
java gson
Is it possible to determine whether a GSON JsonElement
instance is an integer or is it a float?
I'm able to determine whether it's a number:
JsonElement value = ...
boolean isNumber = value.getAsJsonPrimitive().isNumber();
But how to determine if it's an integer or a float, so I can subsequently use the correct conversion method? Either
float f = value.getAsJsonPrimitive().getAsFloat();
or
int i = value.getAsJsonPrimitive().getAsInt();
Edit: The other question may answer why this may be not implemented in GSON, but this question definitely isn't its duplicate.
java gson
java gson
edited Nov 13 '18 at 15:08
yper
asked Nov 13 '18 at 15:00
yperyper
2,49242535
2,49242535
2
Possible duplicate of How to prevent Gson from expressing integers as floats
– lucidbrot
Nov 13 '18 at 15:03
@lucidbrot I don't see how this is a duplicate.
– yper
Nov 13 '18 at 15:09
quoting: "Since JSON doesn't distinguish between integer and floating point fields Gson has to default to Float/Double for numeric fields.". Sounds to me like exactly what you're asking about
– lucidbrot
Nov 13 '18 at 15:15
@lucidbrot I see this answer as being orthogonal to my question. Whatever the way Gson defaults numbers, there could be a way of it answering whether the number essentially "contains a dot" or not.
– yper
Nov 13 '18 at 16:29
Okay, then great you've clarified that it's not a dupe
– lucidbrot
Nov 13 '18 at 16:33
add a comment |
2
Possible duplicate of How to prevent Gson from expressing integers as floats
– lucidbrot
Nov 13 '18 at 15:03
@lucidbrot I don't see how this is a duplicate.
– yper
Nov 13 '18 at 15:09
quoting: "Since JSON doesn't distinguish between integer and floating point fields Gson has to default to Float/Double for numeric fields.". Sounds to me like exactly what you're asking about
– lucidbrot
Nov 13 '18 at 15:15
@lucidbrot I see this answer as being orthogonal to my question. Whatever the way Gson defaults numbers, there could be a way of it answering whether the number essentially "contains a dot" or not.
– yper
Nov 13 '18 at 16:29
Okay, then great you've clarified that it's not a dupe
– lucidbrot
Nov 13 '18 at 16:33
2
2
Possible duplicate of How to prevent Gson from expressing integers as floats
– lucidbrot
Nov 13 '18 at 15:03
Possible duplicate of How to prevent Gson from expressing integers as floats
– lucidbrot
Nov 13 '18 at 15:03
@lucidbrot I don't see how this is a duplicate.
– yper
Nov 13 '18 at 15:09
@lucidbrot I don't see how this is a duplicate.
– yper
Nov 13 '18 at 15:09
quoting: "Since JSON doesn't distinguish between integer and floating point fields Gson has to default to Float/Double for numeric fields.". Sounds to me like exactly what you're asking about
– lucidbrot
Nov 13 '18 at 15:15
quoting: "Since JSON doesn't distinguish between integer and floating point fields Gson has to default to Float/Double for numeric fields.". Sounds to me like exactly what you're asking about
– lucidbrot
Nov 13 '18 at 15:15
@lucidbrot I see this answer as being orthogonal to my question. Whatever the way Gson defaults numbers, there could be a way of it answering whether the number essentially "contains a dot" or not.
– yper
Nov 13 '18 at 16:29
@lucidbrot I see this answer as being orthogonal to my question. Whatever the way Gson defaults numbers, there could be a way of it answering whether the number essentially "contains a dot" or not.
– yper
Nov 13 '18 at 16:29
Okay, then great you've clarified that it's not a dupe
– lucidbrot
Nov 13 '18 at 16:33
Okay, then great you've clarified that it's not a dupe
– lucidbrot
Nov 13 '18 at 16:33
add a comment |
3 Answers
3
active
oldest
votes
use something like, and so if return json object is an instance of float or integer you can then apply the required get:
JSONObject jObj = new JSONObject(jString);
Object aObj = jObj.get("a");
if(aObj instanceof Integer)
System.out.println(aObj);
This does not work sincejObj.get("a")
returnsJsonElement
which is neverInteger
orFloat
(i.e.Integer
is not a child ofJsonElement
).
– yper
Nov 13 '18 at 16:41
This is a direct copy from here. At least link give credit to the original answer
– Eamon Scullion
Nov 13 '18 at 17:02
Additionally, this answer usesorg.json.simple.*
and not Gson.
– yper
Nov 13 '18 at 17:14
add a comment |
The only way I've found so far is using regex on a string:
if (value.getAsJsonPrimitive().isNumber())
String num = value.getAsString();
boolean isFloat = num.matches("[-+]?[0-9]*\.[0-9]+");
if (isFloat)
System.out.println("FLOAT");
else
System.out.println("INTEGER");
This correctly determines 123 as integer, and both 123.45 and 123.0 as floats.
add a comment |
From GSON API docs:
Class JsonElement
...
A class representing an element of Json. It could either be a JsonObject, a JsonArray, a JsonPrimitive or a JsonNull.
and for example:
getAsInt()
convenience method to get this element as a primitive integer value.
JsonElement
is not either int
or float
. You just have the methods to get the value in it somehow casted to some object / primitive.
So your question might better set to something like
Howto check if JsonElement
value can be constructed as int
or as float
or something like that.
Short answer: it is possible but you need to do it by yourself by checking if from value can be constructed something you need, like other answers suggest.
Gson can not know (of course it can try to guess) what is the type of the object some name holds in JSON. It could be also DateTime
serialize to ticks.
Whern using gson.fromJson(...)
I would register type adapter like:
JsonDeserializer<Number> numDeserializer = new JsonDeserializer<Number>()
@Override
public Number deserialize(JsonElement json, Type typeOfT
,JsonDeserializationContext context) throws JsonParseException
try
return new Integer(json.getAsString());
catch (NumberFormatException nfe)
return json.getAsFloat();
;
and have the corresponding field be Number
in my class where i deserialze it.
or if using JsonParser
put similar try catch
-block or helper method alike.
But of course these are just some use cases / possibilities.
This doesn't work. DoinggetAsInt()
on aJsonElement
that is a float 123.45, does not throw but insteads returns an integer 123.
– yper
Nov 13 '18 at 16:49
@yper Well, what can I say. I haveGson
v2.8.5 and getjava.lang.NumberFormatException: For input string: "2.0"
, for example. Maybe it behaves differently outside ofJsonAdapter
. Anyway my point in aswer is elsewhere. Having my GSON not lenient also.
– pirho
Nov 13 '18 at 17:01
getAsInt()
doesn't throw in my environment, which is Gson 2.7, Java 1.7, Linux.
– yper
Nov 13 '18 at 17:10
Ok, I'll try with 2.7.(0?) just to see if there is any difference. BTW: based on your answer you are actually asking something that is not in any way tightly coupled to GSON but just a String rexgexp... And there are so many of those questions with answers in SO.
– pirho
Nov 13 '18 at 17:12
I've used regex as a last resort, I'm happy to accept a better answer that uses "pure" Gson / Java and actually works.
– yper
Nov 13 '18 at 17:16
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283825%2fis-jsonelement-integer-or-float%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
use something like, and so if return json object is an instance of float or integer you can then apply the required get:
JSONObject jObj = new JSONObject(jString);
Object aObj = jObj.get("a");
if(aObj instanceof Integer)
System.out.println(aObj);
This does not work sincejObj.get("a")
returnsJsonElement
which is neverInteger
orFloat
(i.e.Integer
is not a child ofJsonElement
).
– yper
Nov 13 '18 at 16:41
This is a direct copy from here. At least link give credit to the original answer
– Eamon Scullion
Nov 13 '18 at 17:02
Additionally, this answer usesorg.json.simple.*
and not Gson.
– yper
Nov 13 '18 at 17:14
add a comment |
use something like, and so if return json object is an instance of float or integer you can then apply the required get:
JSONObject jObj = new JSONObject(jString);
Object aObj = jObj.get("a");
if(aObj instanceof Integer)
System.out.println(aObj);
This does not work sincejObj.get("a")
returnsJsonElement
which is neverInteger
orFloat
(i.e.Integer
is not a child ofJsonElement
).
– yper
Nov 13 '18 at 16:41
This is a direct copy from here. At least link give credit to the original answer
– Eamon Scullion
Nov 13 '18 at 17:02
Additionally, this answer usesorg.json.simple.*
and not Gson.
– yper
Nov 13 '18 at 17:14
add a comment |
use something like, and so if return json object is an instance of float or integer you can then apply the required get:
JSONObject jObj = new JSONObject(jString);
Object aObj = jObj.get("a");
if(aObj instanceof Integer)
System.out.println(aObj);
use something like, and so if return json object is an instance of float or integer you can then apply the required get:
JSONObject jObj = new JSONObject(jString);
Object aObj = jObj.get("a");
if(aObj instanceof Integer)
System.out.println(aObj);
answered Nov 13 '18 at 15:09
Ptyler649Ptyler649
841
841
This does not work sincejObj.get("a")
returnsJsonElement
which is neverInteger
orFloat
(i.e.Integer
is not a child ofJsonElement
).
– yper
Nov 13 '18 at 16:41
This is a direct copy from here. At least link give credit to the original answer
– Eamon Scullion
Nov 13 '18 at 17:02
Additionally, this answer usesorg.json.simple.*
and not Gson.
– yper
Nov 13 '18 at 17:14
add a comment |
This does not work sincejObj.get("a")
returnsJsonElement
which is neverInteger
orFloat
(i.e.Integer
is not a child ofJsonElement
).
– yper
Nov 13 '18 at 16:41
This is a direct copy from here. At least link give credit to the original answer
– Eamon Scullion
Nov 13 '18 at 17:02
Additionally, this answer usesorg.json.simple.*
and not Gson.
– yper
Nov 13 '18 at 17:14
This does not work since
jObj.get("a")
returns JsonElement
which is never Integer
or Float
(i.e. Integer
is not a child of JsonElement
).– yper
Nov 13 '18 at 16:41
This does not work since
jObj.get("a")
returns JsonElement
which is never Integer
or Float
(i.e. Integer
is not a child of JsonElement
).– yper
Nov 13 '18 at 16:41
This is a direct copy from here. At least link give credit to the original answer
– Eamon Scullion
Nov 13 '18 at 17:02
This is a direct copy from here. At least link give credit to the original answer
– Eamon Scullion
Nov 13 '18 at 17:02
Additionally, this answer uses
org.json.simple.*
and not Gson.– yper
Nov 13 '18 at 17:14
Additionally, this answer uses
org.json.simple.*
and not Gson.– yper
Nov 13 '18 at 17:14
add a comment |
The only way I've found so far is using regex on a string:
if (value.getAsJsonPrimitive().isNumber())
String num = value.getAsString();
boolean isFloat = num.matches("[-+]?[0-9]*\.[0-9]+");
if (isFloat)
System.out.println("FLOAT");
else
System.out.println("INTEGER");
This correctly determines 123 as integer, and both 123.45 and 123.0 as floats.
add a comment |
The only way I've found so far is using regex on a string:
if (value.getAsJsonPrimitive().isNumber())
String num = value.getAsString();
boolean isFloat = num.matches("[-+]?[0-9]*\.[0-9]+");
if (isFloat)
System.out.println("FLOAT");
else
System.out.println("INTEGER");
This correctly determines 123 as integer, and both 123.45 and 123.0 as floats.
add a comment |
The only way I've found so far is using regex on a string:
if (value.getAsJsonPrimitive().isNumber())
String num = value.getAsString();
boolean isFloat = num.matches("[-+]?[0-9]*\.[0-9]+");
if (isFloat)
System.out.println("FLOAT");
else
System.out.println("INTEGER");
This correctly determines 123 as integer, and both 123.45 and 123.0 as floats.
The only way I've found so far is using regex on a string:
if (value.getAsJsonPrimitive().isNumber())
String num = value.getAsString();
boolean isFloat = num.matches("[-+]?[0-9]*\.[0-9]+");
if (isFloat)
System.out.println("FLOAT");
else
System.out.println("INTEGER");
This correctly determines 123 as integer, and both 123.45 and 123.0 as floats.
answered Nov 13 '18 at 17:07
yperyper
2,49242535
2,49242535
add a comment |
add a comment |
From GSON API docs:
Class JsonElement
...
A class representing an element of Json. It could either be a JsonObject, a JsonArray, a JsonPrimitive or a JsonNull.
and for example:
getAsInt()
convenience method to get this element as a primitive integer value.
JsonElement
is not either int
or float
. You just have the methods to get the value in it somehow casted to some object / primitive.
So your question might better set to something like
Howto check if JsonElement
value can be constructed as int
or as float
or something like that.
Short answer: it is possible but you need to do it by yourself by checking if from value can be constructed something you need, like other answers suggest.
Gson can not know (of course it can try to guess) what is the type of the object some name holds in JSON. It could be also DateTime
serialize to ticks.
Whern using gson.fromJson(...)
I would register type adapter like:
JsonDeserializer<Number> numDeserializer = new JsonDeserializer<Number>()
@Override
public Number deserialize(JsonElement json, Type typeOfT
,JsonDeserializationContext context) throws JsonParseException
try
return new Integer(json.getAsString());
catch (NumberFormatException nfe)
return json.getAsFloat();
;
and have the corresponding field be Number
in my class where i deserialze it.
or if using JsonParser
put similar try catch
-block or helper method alike.
But of course these are just some use cases / possibilities.
This doesn't work. DoinggetAsInt()
on aJsonElement
that is a float 123.45, does not throw but insteads returns an integer 123.
– yper
Nov 13 '18 at 16:49
@yper Well, what can I say. I haveGson
v2.8.5 and getjava.lang.NumberFormatException: For input string: "2.0"
, for example. Maybe it behaves differently outside ofJsonAdapter
. Anyway my point in aswer is elsewhere. Having my GSON not lenient also.
– pirho
Nov 13 '18 at 17:01
getAsInt()
doesn't throw in my environment, which is Gson 2.7, Java 1.7, Linux.
– yper
Nov 13 '18 at 17:10
Ok, I'll try with 2.7.(0?) just to see if there is any difference. BTW: based on your answer you are actually asking something that is not in any way tightly coupled to GSON but just a String rexgexp... And there are so many of those questions with answers in SO.
– pirho
Nov 13 '18 at 17:12
I've used regex as a last resort, I'm happy to accept a better answer that uses "pure" Gson / Java and actually works.
– yper
Nov 13 '18 at 17:16
|
show 4 more comments
From GSON API docs:
Class JsonElement
...
A class representing an element of Json. It could either be a JsonObject, a JsonArray, a JsonPrimitive or a JsonNull.
and for example:
getAsInt()
convenience method to get this element as a primitive integer value.
JsonElement
is not either int
or float
. You just have the methods to get the value in it somehow casted to some object / primitive.
So your question might better set to something like
Howto check if JsonElement
value can be constructed as int
or as float
or something like that.
Short answer: it is possible but you need to do it by yourself by checking if from value can be constructed something you need, like other answers suggest.
Gson can not know (of course it can try to guess) what is the type of the object some name holds in JSON. It could be also DateTime
serialize to ticks.
Whern using gson.fromJson(...)
I would register type adapter like:
JsonDeserializer<Number> numDeserializer = new JsonDeserializer<Number>()
@Override
public Number deserialize(JsonElement json, Type typeOfT
,JsonDeserializationContext context) throws JsonParseException
try
return new Integer(json.getAsString());
catch (NumberFormatException nfe)
return json.getAsFloat();
;
and have the corresponding field be Number
in my class where i deserialze it.
or if using JsonParser
put similar try catch
-block or helper method alike.
But of course these are just some use cases / possibilities.
This doesn't work. DoinggetAsInt()
on aJsonElement
that is a float 123.45, does not throw but insteads returns an integer 123.
– yper
Nov 13 '18 at 16:49
@yper Well, what can I say. I haveGson
v2.8.5 and getjava.lang.NumberFormatException: For input string: "2.0"
, for example. Maybe it behaves differently outside ofJsonAdapter
. Anyway my point in aswer is elsewhere. Having my GSON not lenient also.
– pirho
Nov 13 '18 at 17:01
getAsInt()
doesn't throw in my environment, which is Gson 2.7, Java 1.7, Linux.
– yper
Nov 13 '18 at 17:10
Ok, I'll try with 2.7.(0?) just to see if there is any difference. BTW: based on your answer you are actually asking something that is not in any way tightly coupled to GSON but just a String rexgexp... And there are so many of those questions with answers in SO.
– pirho
Nov 13 '18 at 17:12
I've used regex as a last resort, I'm happy to accept a better answer that uses "pure" Gson / Java and actually works.
– yper
Nov 13 '18 at 17:16
|
show 4 more comments
From GSON API docs:
Class JsonElement
...
A class representing an element of Json. It could either be a JsonObject, a JsonArray, a JsonPrimitive or a JsonNull.
and for example:
getAsInt()
convenience method to get this element as a primitive integer value.
JsonElement
is not either int
or float
. You just have the methods to get the value in it somehow casted to some object / primitive.
So your question might better set to something like
Howto check if JsonElement
value can be constructed as int
or as float
or something like that.
Short answer: it is possible but you need to do it by yourself by checking if from value can be constructed something you need, like other answers suggest.
Gson can not know (of course it can try to guess) what is the type of the object some name holds in JSON. It could be also DateTime
serialize to ticks.
Whern using gson.fromJson(...)
I would register type adapter like:
JsonDeserializer<Number> numDeserializer = new JsonDeserializer<Number>()
@Override
public Number deserialize(JsonElement json, Type typeOfT
,JsonDeserializationContext context) throws JsonParseException
try
return new Integer(json.getAsString());
catch (NumberFormatException nfe)
return json.getAsFloat();
;
and have the corresponding field be Number
in my class where i deserialze it.
or if using JsonParser
put similar try catch
-block or helper method alike.
But of course these are just some use cases / possibilities.
From GSON API docs:
Class JsonElement
...
A class representing an element of Json. It could either be a JsonObject, a JsonArray, a JsonPrimitive or a JsonNull.
and for example:
getAsInt()
convenience method to get this element as a primitive integer value.
JsonElement
is not either int
or float
. You just have the methods to get the value in it somehow casted to some object / primitive.
So your question might better set to something like
Howto check if JsonElement
value can be constructed as int
or as float
or something like that.
Short answer: it is possible but you need to do it by yourself by checking if from value can be constructed something you need, like other answers suggest.
Gson can not know (of course it can try to guess) what is the type of the object some name holds in JSON. It could be also DateTime
serialize to ticks.
Whern using gson.fromJson(...)
I would register type adapter like:
JsonDeserializer<Number> numDeserializer = new JsonDeserializer<Number>()
@Override
public Number deserialize(JsonElement json, Type typeOfT
,JsonDeserializationContext context) throws JsonParseException
try
return new Integer(json.getAsString());
catch (NumberFormatException nfe)
return json.getAsFloat();
;
and have the corresponding field be Number
in my class where i deserialze it.
or if using JsonParser
put similar try catch
-block or helper method alike.
But of course these are just some use cases / possibilities.
edited Nov 13 '18 at 17:36
answered Nov 13 '18 at 16:31
pirhopirho
4,065101830
4,065101830
This doesn't work. DoinggetAsInt()
on aJsonElement
that is a float 123.45, does not throw but insteads returns an integer 123.
– yper
Nov 13 '18 at 16:49
@yper Well, what can I say. I haveGson
v2.8.5 and getjava.lang.NumberFormatException: For input string: "2.0"
, for example. Maybe it behaves differently outside ofJsonAdapter
. Anyway my point in aswer is elsewhere. Having my GSON not lenient also.
– pirho
Nov 13 '18 at 17:01
getAsInt()
doesn't throw in my environment, which is Gson 2.7, Java 1.7, Linux.
– yper
Nov 13 '18 at 17:10
Ok, I'll try with 2.7.(0?) just to see if there is any difference. BTW: based on your answer you are actually asking something that is not in any way tightly coupled to GSON but just a String rexgexp... And there are so many of those questions with answers in SO.
– pirho
Nov 13 '18 at 17:12
I've used regex as a last resort, I'm happy to accept a better answer that uses "pure" Gson / Java and actually works.
– yper
Nov 13 '18 at 17:16
|
show 4 more comments
This doesn't work. DoinggetAsInt()
on aJsonElement
that is a float 123.45, does not throw but insteads returns an integer 123.
– yper
Nov 13 '18 at 16:49
@yper Well, what can I say. I haveGson
v2.8.5 and getjava.lang.NumberFormatException: For input string: "2.0"
, for example. Maybe it behaves differently outside ofJsonAdapter
. Anyway my point in aswer is elsewhere. Having my GSON not lenient also.
– pirho
Nov 13 '18 at 17:01
getAsInt()
doesn't throw in my environment, which is Gson 2.7, Java 1.7, Linux.
– yper
Nov 13 '18 at 17:10
Ok, I'll try with 2.7.(0?) just to see if there is any difference. BTW: based on your answer you are actually asking something that is not in any way tightly coupled to GSON but just a String rexgexp... And there are so many of those questions with answers in SO.
– pirho
Nov 13 '18 at 17:12
I've used regex as a last resort, I'm happy to accept a better answer that uses "pure" Gson / Java and actually works.
– yper
Nov 13 '18 at 17:16
This doesn't work. Doing
getAsInt()
on a JsonElement
that is a float 123.45, does not throw but insteads returns an integer 123.– yper
Nov 13 '18 at 16:49
This doesn't work. Doing
getAsInt()
on a JsonElement
that is a float 123.45, does not throw but insteads returns an integer 123.– yper
Nov 13 '18 at 16:49
@yper Well, what can I say. I have
Gson
v2.8.5 and get java.lang.NumberFormatException: For input string: "2.0"
, for example. Maybe it behaves differently outside of JsonAdapter
. Anyway my point in aswer is elsewhere. Having my GSON not lenient also.– pirho
Nov 13 '18 at 17:01
@yper Well, what can I say. I have
Gson
v2.8.5 and get java.lang.NumberFormatException: For input string: "2.0"
, for example. Maybe it behaves differently outside of JsonAdapter
. Anyway my point in aswer is elsewhere. Having my GSON not lenient also.– pirho
Nov 13 '18 at 17:01
getAsInt()
doesn't throw in my environment, which is Gson 2.7, Java 1.7, Linux.– yper
Nov 13 '18 at 17:10
getAsInt()
doesn't throw in my environment, which is Gson 2.7, Java 1.7, Linux.– yper
Nov 13 '18 at 17:10
Ok, I'll try with 2.7.(0?) just to see if there is any difference. BTW: based on your answer you are actually asking something that is not in any way tightly coupled to GSON but just a String rexgexp... And there are so many of those questions with answers in SO.
– pirho
Nov 13 '18 at 17:12
Ok, I'll try with 2.7.(0?) just to see if there is any difference. BTW: based on your answer you are actually asking something that is not in any way tightly coupled to GSON but just a String rexgexp... And there are so many of those questions with answers in SO.
– pirho
Nov 13 '18 at 17:12
I've used regex as a last resort, I'm happy to accept a better answer that uses "pure" Gson / Java and actually works.
– yper
Nov 13 '18 at 17:16
I've used regex as a last resort, I'm happy to accept a better answer that uses "pure" Gson / Java and actually works.
– yper
Nov 13 '18 at 17:16
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283825%2fis-jsonelement-integer-or-float%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Possible duplicate of How to prevent Gson from expressing integers as floats
– lucidbrot
Nov 13 '18 at 15:03
@lucidbrot I don't see how this is a duplicate.
– yper
Nov 13 '18 at 15:09
quoting: "Since JSON doesn't distinguish between integer and floating point fields Gson has to default to Float/Double for numeric fields.". Sounds to me like exactly what you're asking about
– lucidbrot
Nov 13 '18 at 15:15
@lucidbrot I see this answer as being orthogonal to my question. Whatever the way Gson defaults numbers, there could be a way of it answering whether the number essentially "contains a dot" or not.
– yper
Nov 13 '18 at 16:29
Okay, then great you've clarified that it's not a dupe
– lucidbrot
Nov 13 '18 at 16:33