@Override public JsonObject flatten(@Nonnull String separator) { JsonObject o = new SimpleIntMapJsonObject(); flatten(o,"",separator,this); return o; }
@Override public Object clone() { return deepClone(); }
@Override public boolean containsKey(Object key) { return get(key) != null; }
@Override protected JsonObject createNew() { return new SimpleIntMapJsonObject(); }
@Override public int hashCode() { if (idField != null) { JsonElement jsonElement = get(idField); if (jsonElement != null) { return jsonElement.hashCode(); } } int hashCode = 23; Set<Entry<String, JsonElement>> entrySet = entrySet(); for (Entry<String, JsonElement> entry : entrySet) { JsonElement value = entry.getValue(); if (value != null) { // skip null entries hashCode = hashCode * entry.getKey().hashCode() * value.hashCode(); } } return hashCode; }
@SuppressWarnings("rawtypes") public SimpleIntMapJsonObject(@Nonnull Map existing) { Iterator iterator = existing.entrySet().iterator(); while (iterator.hasNext()) { Entry entry = (Entry) iterator.next(); put(entry.getKey().toString(), fromObject(entry.getValue())); } }
@Override public boolean equals(Object o) { if (o == null) { return false; } if (!(o instanceof IJsonObject)) { return false; } IJsonObject object = (IJsonObject) o; if (object.entrySet().size() != entrySet().size()) { return false; } Set<Entry<String, JsonElement>> es = entrySet(); for (Entry<String, JsonElement> entry : es) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (!value.equals(object.get(key))) { return false; } } return true; }
@Override public boolean isEmpty() { return keySet().size() == 0; }
@Override void writeObject(java.io.ObjectOutputStream out) throws IOException { // when using object serialization, write the json bytes byte[] bytes = toString().getBytes(StandardCharsets.UTF_8); out.writeInt(bytes.length); out.write(bytes); }
public boolean startObject() { isObject = true; if(settings.useEfficientStringBasedJsonObject()) { stack.add(new SimpleIntMapJsonObject()); } else { stack.add(new JsonObject()); } return true; }
@Override void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { // when deserializing, parse the json string try { int length = in.readInt(); byte[] buf = new byte[length]; in.readFully(buf); if (parser == null) { // create it lazily, static so won't increase object size parser = new JsonParser(); } JsonElement o = parser.parse(new String(buf, StandardCharsets.UTF_8)); Field f = getClass().getDeclaredField("intMap"); f.setAccessible(true); f.set(this, new SimpleIntKeyMap<>()); for (Entry<String, JsonElement> e : o.asObject().entrySet()) { put(e.getKey(), e.getValue()); } } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { throw new IllegalStateException(e); } } }
@Override public void removeEmpty() { Iterator<java.util.Map.Entry<String, JsonElement>> iterator = entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); JsonElement element = entry.getValue(); if (element.isEmpty() && !element.isObject()) { iterator.remove(); } else { element.removeEmpty(); } } }
@DataProvider public Object[][] objectConstructors() { return new Supplier[][]{ {() -> new JsonObject()}, {() -> new MapBasedJsonObject()}, {() -> new SimpleIntMapJsonObject()} }; }