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

cn.allbs.hj212.deser.MapSegmentDeserializer Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package cn.allbs.hj212.deser;

import cn.allbs.hj212.config.SegmentParser;
import cn.allbs.hj212.config.SegmentToken;
import cn.allbs.hj212.exception.SegmentFormatException;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 功能:
 *
 * @author chenQi
 */
public class MapSegmentDeserializer
        implements SegmentDeserializer> {


    protected final SegmentDeserializer _valueDeserializer;
    protected final SegmentDeserializer _stringValueDeserializer;

    public MapSegmentDeserializer() {
        _valueDeserializer = new MapValueSegmentDeserializer(this);
        _stringValueDeserializer = new StringSegmentDeserializer();
    }

    public MapSegmentDeserializer(SegmentDeserializer valueDeserializer) {
        _valueDeserializer = valueDeserializer;
        _stringValueDeserializer = new StringSegmentDeserializer();
    }


    @Override
    public Map deserialize(SegmentParser parser) throws IOException, SegmentFormatException {
        if(parser.currentToken() == null){
            parser.initToken();
        }

        Map result = new LinkedHashMap<>();
        readMap(parser,result);
        return result;
    }


    private void readMap(SegmentParser parser, Map result) throws IOException, SegmentFormatException {
        String key = parser.readKey();
        for (; key != null; key = parser.readKey()) {
            SegmentToken token = parser.currentToken();
            Object value = null;

            switch (token){
                case NOT_AVAILABLE:
                    //end
                    return;
                case END_PART_KEY:
                    //NOT possible
                    assert false;
                case END_SUB_ENTRY:
                case END_ENTRY:
                case END_OBJECT_VALUE:
                    //NULL value
                    break;
                case END_KEY:
                case START_OBJECT_VALUE:
                    //Normal value
                    value = _valueDeserializer.deserialize(parser);
                    break;
            }
            result.put(key, value);
        }
    }

}