package com.brilliance.mda.support.jakson.serialize;

import com.brilliance.mda.runtime.mda.IStream;
import com.brilliance.mda.runtime.mda.impl.StreamImpl;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.util.List;

public class IStreamDeSerialized extends StdDeserializer<IStream> {
    public IStreamDeSerialized() {
        this(null);
    }
    protected IStreamDeSerialized(Class<?> vc) {
        super(vc);
    }

    @Override
    public IStream deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        ObjectCodec codec = jp.getCodec();
        JsonNode node = codec.readTree(jp);
        //IStream目前统一用StreamImpl构造，后续新增类型时，再逐步补充
        StreamImpl stream = new StreamImpl();
        if(node.has("rows"))
        {
            JsonNode s = node.get("rows");
            List<String> tempRows = codec.treeToValue(s,List.class);
            stream.getRows().addAll(tempRows);
        }
        return stream;
    }
}
