package com.libreecm.commonAPI.auxiliars;

import java.io.IOException;
import java.util.ArrayList;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

public class JsonStringListDeserializer extends JsonDeserializer<ArrayList<String>> {
	
	
    @Override

    public ArrayList<String> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    	ArrayList<String> result = new ArrayList<String>();
    	//an example of jp.getText() could be ["HR","IT","SC"] or ["HR"] where there's a deserialitzation problem
    	//First will be ["HR, last will be "SC"] let's clean that
    	String input= jp.getText();
    	if (input.startsWith("[\""))
    		input=input.substring(2, input.length()-1);
    	if (input.endsWith("\"]"))
    		input=input.substring(0,input.length()-3);
    	
    	//now we have: HR","IT","SC
    	//if any element has char " it will be no problem because it will be \"
    	for (String element: input.split("\", \"")){
    		result.add(element);
    	}
    	return result;
    }


}