JsonValueUtil.groovy revision 548846fe158900a483ca91f47603c6bb6fde9b47
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch/*
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch *
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * Copyright 2015 ForgeRock AS
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch *
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * The contents of this file are subject to the terms
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * of the Common Development and Distribution License
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * (the License). You may not use this file except in
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * compliance with the License.
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch *
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * You can obtain a copy of the License at
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * http://forgerock.org/license/CDDLv1.0.html
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * See the License for the specific language governing
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * permission and limitations under the License.
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch *
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * When distributing Covered Code, include this CDDL
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * Header Notice in each file and include the License file
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * at http://forgerock.org/license/CDDLv1.0.html
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * If applicable, add the following below the CDDL Header,
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * with the fields enclosed by brackets [] replaced by
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * your own identifying information:
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch * "Portions Copyrighted [year] [name of copyright owner]"
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch */
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branchimport static org.forgerock.json.JsonValue.*;
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branchimport org.forgerock.json.JsonValue
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branchimport org.forgerock.http.util.Json;
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branchpublic class JsonValueUtil {
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch public static JsonValue fromEntries(Map.Entry<String,Object>[] entries) {
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch JsonValue json = json(object());
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch for (Map.Entry<String,Object> entry : entries) {
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch if (entry.getKey() != null && entry.getValue() != null) {
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch json.add(entry.getKey(), entry.getValue());
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch }
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch }
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch return json;
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch }
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch public static JsonValue fromJsonString(String string) {
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch return string != null && string.length() > 0 ? json(Json.readJson(string)) : null;
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch }
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch public static Boolean booleanFromString(String string) {
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch try {
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch return Boolean.valueOf(string);
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch } catch (Exception e) {
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch return Boolean.FALSE;
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch }
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch }
548846fe158900a483ca91f47603c6bb6fde9b47Jon Branch}