
com.launchdarkly.client.value.LDValueObject Maven / Gradle / Ivy
package com.launchdarkly.client.value;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.Map;
@JsonAdapter(LDValueTypeAdapter.class)
final class LDValueObject extends LDValue {
private static final LDValueObject EMPTY = new LDValueObject(ImmutableMap.of());
private final Map map;
static LDValueObject fromMap(Map map) {
return map.isEmpty() ? EMPTY : new LDValueObject(map);
}
private LDValueObject(Map map) {
this.map = map;
}
public LDValueType getType() {
return LDValueType.OBJECT;
}
@Override
public int size() {
return map.size();
}
@Override
public Iterable keys() {
return map.keySet();
}
@Override
public Iterable values() {
return map.values();
}
@Override
public LDValue get(String name) {
LDValue v = map.get(name);
return v == null ? ofNull() : v;
}
@Override
void write(JsonWriter writer) throws IOException {
writer.beginObject();
for (Map.Entry e: map.entrySet()) {
writer.name(e.getKey());
e.getValue().write(writer);
}
writer.endObject();
}
@Override
@SuppressWarnings("deprecation")
JsonElement computeJsonElement() {
JsonObject o = new JsonObject();
for (String key: map.keySet()) {
o.add(key, map.get(key).asUnsafeJsonElement());
}
return o;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy