net.motel.json.parser.JsonParser.java Source code

Java tutorial

Introduction

Here is the source code for net.motel.json.parser.JsonParser.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.motel.json.parser;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonStreamParser;
import java.io.ByteArrayInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.logging.Level;
import java.util.logging.Logger;
import static java.util.stream.Collectors.toList;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import net.motel.entity.Motel;

public class JsonParser {

    Gson gson;

    public JsonParser() {
        gson = new Gson();
    }

    //Load the json test file
    public List<Motel> getMotels() {
        List<Motel> motels = null;

        Reader reader = new InputStreamReader(getDummyJsonData());

        JsonStreamParser parser = new JsonStreamParser(reader);

        //Puts the JsonArray into a JsonElement Stream
        Stream<JsonElement> jsonStream = StreamSupport
                .stream(Spliterators.spliteratorUnknownSize(parser, Spliterator.ORDERED), false);

        motels = jsonStream.map((arr) -> { //map the JsonElement to a JsonArray
            return arr.getAsJsonArray();
        }).flatMap((smallArr) -> { // map the contents of the JsonArray into a Stream
            return StreamSupport.stream(smallArr.spliterator(), false);
        }).map((element) -> { //Map array contents to a Motel class
            return gson.fromJson(element, Motel.class);
        }).collect(toList()); //Collect all mapped Motel classes into a list.

        return motels;
    }

    private InputStream getDummyJsonData() {
        String dummyData = "[" + "    {" + "        \"motelName\":\"Chariott Hotel\","
                + "        \"city\":\"San Jose\"," + "        \"state\":\"California\","
                + "        \"zipCode\":\"984522\"," + "        \"maxOccupancy\":\"100\","
                + "        \"availablerooms\":\"10\"," + "        \"numberOfBedsPerRoom\":\"2\","
                + "        \"smoking\":\"NO\","
                + "        \"amenities\":[\"tv\",\"microwave\",\"refrigerator\",\"cable\",\"bathroom\",\"wi-fi\"]"
                + "    }," + "    {" + "        \"motelName\":\"Chariott Hotel\"," + "        \"city\":\"Seattle\","
                + "        \"state\":\"Washington\"," + "        \"zipCode\":\"98201\","
                + "        \"maxOccupancy\":\"80\"," + "        \"availablerooms\":\"30\","
                + "        \"numberOfBedsPerRoom\":\"2\"," + "        \"smoking\":\"NO\","
                + "        \"amenities\":[\"tv\",\"microwave\",\"refrigerator\",\"cable\",\"bathroom\",\"wi-fi\"]"
                + "    }," + "    {" + "        \"motelName\":\"Best Holiday Hotel\","
                + "        \"city\":\"Portland\"," + "        \"state\":\"Oregon\","
                + "        \"zipCode\":\"92009\"," + "        \"maxOccupancy\":\"75\","
                + "        \"availablerooms\":\"10\"," + "        \"numberOfBedsPerRoom\":\"2\","
                + "        \"smoking\":\"NO\","
                + "        \"amenities\":[\"tv\",\"microwave\",\"refrigerator\",\"cable\",\"bathroom\",\"wi-fi\"]"
                + "    }," + "    {" + "        \"motelName\":\"The 5 Seasons Hotel\","
                + "        \"city\":\"Montgomery\"," + "        \"state\":\"Alabama\","
                + "        \"zipCode\":\"12009\"," + "        \"maxOccupancy\":\"475\","
                + "        \"availablerooms\":\"12\"," + "        \"numberOfBedsPerRoom\":\"2\","
                + "        \"smoking\":\"YES\","
                + "        \"amenities\":[\"tv\",\"microwave\",\"refrigerator\",\"cable\",\"bathroom\",\"wi-fi\",\"bar\",\"spa\"]"
                + "    }," + "    {" + "        \"motelName\":\"The 5 Seasons Hotel\","
                + "        \"city\":\"Selma\"," + "        \"state\":\"Alabama\","
                + "        \"zipCode\":\"12019\"," + "        \"maxOccupancy\":\"475\","
                + "        \"availablerooms\":\"0\"," + "        \"numberOfBedsPerRoom\":\"2\","
                + "        \"smoking\":\"YES\","
                + "        \"amenities\":[\"tv\",\"microwave\",\"refrigerator\",\"cable\",\"bathroom\",\"wi-fi\",\"bar\",\"spa\"]"
                + "    }," + "    {" + "        \"motelName\":\"The 5 Seasons Hotel\","
                + "        \"city\":\"Green Bow\"," + "        \"state\":\"Alabama\","
                + "        \"zipCode\":\"12029\"," + "        \"maxOccupancy\":\"475\","
                + "        \"availablerooms\":\"0\"," + "        \"numberOfBedsPerRoom\":\"2\","
                + "        \"smoking\":\"YES\","
                + "        \"amenities\":[\"tv\",\"microwave\",\"refrigerator\",\"cable\",\"bathroom\",\"wi-fi\",\"bar\",\"spa\"]"
                + "    }" + "]";

        return new ByteArrayInputStream(dummyData.getBytes());
    }

}