All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jsoniter.DefaultMapKeyDecoder Maven / Gradle / Ivy

Go to download

jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go

There is a newer version: 0.9.23
Show newest version
package com.jsoniter;

import com.jsoniter.spi.*;

import java.io.IOException;
import java.lang.reflect.Type;

class DefaultMapKeyDecoder implements MapKeyDecoder {

    public static MapKeyDecoder registerOrGetExisting(Type mapKeyType) {
        String cacheKey = JsoniterSpi.getMapKeyDecoderCacheKey(mapKeyType);
        MapKeyDecoder mapKeyDecoder = JsoniterSpi.getMapKeyDecoder(cacheKey);
        if (null != mapKeyDecoder) {
            return mapKeyDecoder;
        }
        mapKeyDecoder = new DefaultMapKeyDecoder(TypeLiteral.create(mapKeyType));
        JsoniterSpi.addNewMapDecoder(cacheKey, mapKeyDecoder);
        return mapKeyDecoder;
    }

    private final TypeLiteral mapKeyTypeLiteral;

    private DefaultMapKeyDecoder(TypeLiteral mapKeyTypeLiteral) {
        this.mapKeyTypeLiteral = mapKeyTypeLiteral;
    }

    @Override
    public Object decode(Slice encodedMapKey) {
        JsonIterator iter = JsonIteratorPool.borrowJsonIterator();
        iter.reset(encodedMapKey);
        try {
            return iter.read(mapKeyTypeLiteral);
        } catch (IOException e) {
            throw new JsonException(e);
        } finally {
            JsonIteratorPool.returnJsonIterator(iter);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy