package com.hubilon.json; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.ToNumberPolicy; import java.util.Map; /** * JSON reader, backed by Gson. Kept as a single call site so the rest of the * app depends on this API rather than on Gson directly. * * ToNumberPolicy.LONG_OR_DOUBLE keeps whole numbers as Long (so re-serializing * a pass-through field like expected.status doesn't turn "200" into "200.0"). */ public final class Json { private static final Gson GSON = new GsonBuilder() .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE) .create(); private Json() { } @SuppressWarnings("unchecked") public static Map parseObject(String text) { try { Map result = GSON.fromJson(text, Map.class); if (result == null) { throw new JsonException("JSON document is empty or 'null'"); } return result; } catch (RuntimeException e) { throw new JsonException(e.getMessage()); } } public static final class JsonException extends RuntimeException { public JsonException(String message) { super(message); } } }