com.backblaze.b2.json.B2JsonConcurrentMapHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of b2-sdk-core Show documentation
Show all versions of b2-sdk-core Show documentation
The core logic for B2 SDK for Java. Does not include any implementations of B2WebApiClient.
/*
* Copyright 2017, Backblaze Inc. All Rights Reserved.
* License https://www.backblaze.com/using_b2_code.html
*/
package com.backblaze.b2.json;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class B2JsonConcurrentMapHandler extends B2JsonNonUrlTypeHandler {
private final B2JsonTypeHandler keyHandler;
private final B2JsonTypeHandler valueHandler;
public B2JsonConcurrentMapHandler(B2JsonTypeHandler keyHandler, B2JsonTypeHandler valueHandler) throws B2JsonException {
if (!keyHandler.isStringInJson()) {
throw new B2JsonException("Map key is not a string: " + keyHandler.getHandledClass());
}
this.keyHandler = keyHandler;
this.valueHandler = valueHandler;
}
public Class getHandledClass() {
return ConcurrentMap.class;
}
public void serialize(ConcurrentMap obj, B2JsonWriter out) throws IOException, B2JsonException {
out.startObject();
for (Map.Entry entry : (Set) obj.entrySet()) {
out.startObjectFieldName();
keyHandler.serialize(entry.getKey(), out);
out.writeText(": ");
B2JsonUtil.serializeMaybeNull(valueHandler, entry.getValue(), out);
}
out.finishObject();
}
public ConcurrentMap deserialize(B2JsonReader in, int options) throws B2JsonException, IOException {
ConcurrentMap result = new ConcurrentHashMap();
if (in.startObjectAndCheckForContents()) {
do {
Object key = keyHandler.deserialize(in, options);
in.skipObjectColon();
Object value = B2JsonUtil.deserializeMaybeNull(valueHandler, in, options);
result.put(key, value);
} while (in.objectHasMoreFields());
}
in.finishObject();
return result;
}
public ConcurrentMap defaultValueForOptional() {
return null;
}
public boolean isStringInJson() {
return false;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy