Remove _id field in the converter and care about if numbers are floating or not.

This commit is contained in:
Olof Larsson 2013-05-01 18:59:05 +02:00
parent eb3fc18f65
commit 9a8a792315
2 changed files with 39 additions and 3 deletions

View File

@ -133,6 +133,8 @@ public class DriverMongo extends DriverAbstract
DBCollection dbcoll = fixColl(coll); DBCollection dbcoll = fixColl(coll);
BasicDBObject raw = (BasicDBObject)dbcoll.findOne(new BasicDBObject(ID_FIELD, id)); BasicDBObject raw = (BasicDBObject)dbcoll.findOne(new BasicDBObject(ID_FIELD, id));
if (raw == null) return null; if (raw == null) return null;
raw.removeField(ID_FIELD);
Long mtime = (Long) raw.removeField(MTIME_FIELD); Long mtime = (Long) raw.removeField(MTIME_FIELD);
JsonElement element = MongoGsonConverter.mongo2GsonObject(raw); JsonElement element = MongoGsonConverter.mongo2GsonObject(raw);

View File

@ -10,6 +10,7 @@ import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.gson.JsonNull; import com.massivecraft.mcore.xlib.gson.JsonNull;
import com.massivecraft.mcore.xlib.gson.JsonObject; import com.massivecraft.mcore.xlib.gson.JsonObject;
import com.massivecraft.mcore.xlib.gson.JsonPrimitive; import com.massivecraft.mcore.xlib.gson.JsonPrimitive;
import com.massivecraft.mcore.xlib.gson.internal.LazilyParsedNumber;
import com.massivecraft.mcore.xlib.mongodb.BasicDBList; import com.massivecraft.mcore.xlib.mongodb.BasicDBList;
import com.massivecraft.mcore.xlib.mongodb.BasicDBObject; import com.massivecraft.mcore.xlib.mongodb.BasicDBObject;
import com.massivecraft.mcore.xlib.mongodb.DBObject; import com.massivecraft.mcore.xlib.mongodb.DBObject;
@ -88,9 +89,42 @@ public final class MongoGsonConverter
{ {
if (inElement.isJsonNull()) return null; if (inElement.isJsonNull()) return null;
JsonPrimitive in = inElement.getAsJsonPrimitive(); JsonPrimitive in = inElement.getAsJsonPrimitive();
if (in.isBoolean()) return in.getAsBoolean();
if (in.isNumber()) return in.getAsNumber().doubleValue(); if (in.isBoolean())
if (in.isString()) return in.getAsString(); {
return in.getAsBoolean();
}
if (in.isNumber())
{
Number number = in.getAsNumber();
boolean floating;
if (number instanceof LazilyParsedNumber)
{
floating = StringUtils.contains(number.toString(), '.');
}
else
{
floating = (number instanceof Double || number instanceof Float);
}
if (floating)
{
return number.doubleValue();
}
else
{
return number.longValue();
}
}
if (in.isString())
{
return in.getAsString();
}
throw new IllegalArgumentException("Unsupported value type for: " + in); throw new IllegalArgumentException("Unsupported value type for: " + in);
} }