001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.util;
021
022import com.google.gson.JsonArray;
023import com.google.gson.JsonObject;
024import com.google.gson.JsonPrimitive;
025
026/**
027 * The whole purpose of his class is to ease construction of a non-trivial gson.JsonObject,
028 * which can't be done the easy way in this case (using a JSON string), because there are
029 * valid regex strings which break gson, as this: ".*\\^Donor$"
030 */
031public class RegexpGsonBuilderUtil {
032
033        private RegexpGsonBuilderUtil() {}
034
035        /**
036         * Builds a json object as this sample:
037         * {"regexp":{" + thePropName + ":{"value":" + theValue + "}}}
038         */
039        public static JsonObject toGson(String thePropName, String theValue) {
040                JsonObject valueJO = new JsonObject();
041                valueJO.add("value", new JsonPrimitive(theValue));
042
043                JsonObject systemValueJO = new JsonObject();
044                systemValueJO.add(thePropName, valueJO);
045
046                JsonObject regexpJO = new JsonObject();
047                regexpJO.add("regexp", systemValueJO);
048
049                JsonArray a = new JsonArray();
050                return regexpJO;
051        }
052}