About the author

Related Articles

One Comment

  1. 1
    TeeBea@email.null'

    TeeBea

    Here is an example using Jackson to serialize your nested lists as json:

    import java.util.ArrayList;
    import java.util.List;
    
    class EmbeddedList
    {
        private List<List<Double>> list = new ArrayList<>();
        public List<List<Double>> getList()
        {
            return list;
        }
    }
    

    and the serializer:

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    
    public class EmbeddedListSerializer 
    {
    
    
    public static void main(String[] args) 
    {
        EmbeddedList saveMe = new EmbeddedList();
        List<Double> firstList = new ArrayList<>();
        firstList.add(0.0);
        firstList.add(0.0);
        firstList.add(1d);
    
        List<Double> secondList = new ArrayList<>();
        secondList.add(32.0);
        secondList.add(0.0);
        secondList.add(1d);
    
        List<Double> thirdList = new ArrayList<>();
        thirdList.add(64.0);
        thirdList.add(0.0);
        thirdList.add(1d);
    
        saveMe.getList().add(firstList);
        saveMe.getList().add(secondList);
        saveMe.getList().add(thirdList);
    
        EmbeddedListSerializer serializer = new EmbeddedListSerializer();
        serializer.saveJson(saveMe);
    
        EmbeddedList loaded = serializer.loadJson();
        for( List<Double> subList : loaded.getList() )
        {
            for( Double d : subList )
            {
                System.out.println( d );
            }
        }
    }
    
    public void saveJson( EmbeddedList embeddedList )
    {
        ObjectMapper mapper = new ObjectMapper();
        try 
        {
            String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(embeddedList);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("list.json"));
            PrintWriter out = new PrintWriter(outputStreamWriter);
            PrintWriter writer = new PrintWriter(out);
            writer.print(json);
            writer.close();
    
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
    
    private EmbeddedList loadJson()
    {
        EmbeddedList list = null;
        try
        {
            ObjectMapper mapper = new ObjectMapper();
            File file = new File("list.json");
            list = mapper.readValue( file, EmbeddedList.class );
        }
        catch( IOException e )
        {
            e.printStackTrace();
        }
        return list;
    }   
    

    }

    which stores the data as:

    {
      "list" : [ [ 0.0, 0.0, 1.0 ], [ 32.0, 0.0, 1.0 ], [ 64.0, 0.0, 1.0 ] ]
    }
    

    If you want to, change the Double to Number in EmbeddedList object, then doubles are serialized as doubles and ints as ints.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2017 SolutionMmyself.com