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

cn.allbs.hj212.deser.StringMapSegmentDeserializer 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 StringMapSegmentDeserializer
        implements SegmentDeserializer> {


    protected final SegmentDeserializer _valueDeserializer;

    public StringMapSegmentDeserializer() {
        _valueDeserializer = 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();
            String 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);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy