Java tutorial
/* * Copyright 2014-2016 Open Networking Laboratory * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.onosproject.north.aaa.api.parser.impl; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.onosproject.north.aaa.api.JsonParserService; import org.onosproject.north.aaa.api.ParseException; import org.onosproject.north.aaa.store.RestAccess; import java.io.IOException; import java.io.InputStream; import java.util.HashSet; import java.util.Set; /** * Parse and build rest access class object from accesses.json file. */ public class RestAccessParser implements JsonParserService<RestAccess> { @Override public Set<RestAccess> parseJson(InputStream stream) throws IOException, ParseException { // begin parsing JSON to Application class ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(stream); JsonParser jp = jsonNode.traverse(); Set<RestAccess> restAccessSet = new HashSet<>(); // continue parsing the token till the end of input is reached while (!jp.isClosed()) { // get the token JsonToken token = jp.nextToken(); // if its the last token then we are done if (token == null) { break; } if (JsonToken.FIELD_NAME.equals(token) && "accesses".equals(jp.getCurrentName())) { token = jp.nextToken(); if (!JsonToken.START_ARRAY.equals(token)) { // bail out throw new ParseException("expected ARRAY after accesses"); } while (true) { token = jp.nextToken(); if (JsonToken.END_ARRAY.equals(token)) { // bail out break; } if (JsonToken.START_OBJECT.equals(token)) { RestAccess restAccess = jsonToRestAccess(jp); restAccessSet.add(restAccess); } } } } return restAccessSet; } public RestAccess jsonToRestAccess(JsonParser jp) throws IOException, ParseException { RestAccess.Builder builder = RestAccess.builder(); while (true) { JsonToken token = jp.nextToken(); if (JsonToken.END_OBJECT.equals(token)) { // bail out break; } if (JsonToken.FIELD_NAME.equals(token) && "accessId".equals(jp.getCurrentName())) { jp.nextToken(); builder.buildeAccessId(jp.getText()); } else if (JsonToken.FIELD_NAME.equals(token) && "accessDesc".equals(jp.getCurrentName())) { jp.nextToken(); builder.buildAccessDesc(jp.getText()); } else if (JsonToken.FIELD_NAME.equals(token) && "restUri".equals(jp.getCurrentName())) { jp.nextToken(); builder.buildRestUri(jp.getText()); } else if (JsonToken.FIELD_NAME.equals(token) && "restMethod".equals(jp.getCurrentName())) { jp.nextToken(); builder.buildRestMethod(jp.getText()); } else if (JsonToken.FIELD_NAME.equals(token) && "restParam".equals(jp.getCurrentName())) { jp.nextToken(); builder.buildRestParam(jp.getText()); } } return builder.buildAll(); } }