001package ball.databind;
002/*-
003 * ##########################################################################
004 * Data Binding Utilities
005 * $Id: JSONBean.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-databind/trunk/src/main/java/ball/databind/JSONBean.java $
007 * %%
008 * Copyright (C) 2016 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import com.fasterxml.jackson.databind.DeserializationFeature;
024import com.fasterxml.jackson.databind.JsonNode;
025import com.fasterxml.jackson.databind.MapperFeature;
026import com.fasterxml.jackson.databind.ObjectMapper;
027import com.fasterxml.jackson.databind.SerializationFeature;
028import java.io.Serializable;
029import lombok.NoArgsConstructor;
030
031import static lombok.AccessLevel.PROTECTED;
032
033/**
034 * Abstract base class for bean implementations that wrap a
035 * {@link JsonNode}.
036 *
037 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
038 * @version $Revision: 5285 $
039 */
040@NoArgsConstructor(access = PROTECTED)
041public abstract class JSONBean implements Serializable {
042    private static final long serialVersionUID = -7720273815805776898L;
043
044    /** @serial */ protected ObjectMapper mapper = ObjectMapperConfiguration.MAPPER;
045    /** @serial */ protected JsonNode node = null;
046
047    /**
048     * Convenience method to call {@link JsonNode#at(String)}.
049     *
050     * @param   expression      The {@link String} respresentation of the
051     *                          {@link com.fasterxml.jackson.core.JsonPointer}.
052     *
053     * @return  {@link JsonNode} that matches given
054     *          {@link com.fasterxml.jackson.core.JsonPointer}: if no match
055     *          exists, will return a {@link JsonNode} for which
056     *          {@link com.fasterxml.jackson.core.TreeNode#isMissingNode()}
057     *          returns {@code true}.
058     */
059    protected JsonNode nodeAt(String expression) {
060        return (expression != null) ? node.at(expression) : null;
061    }
062
063    /**
064     * Convenience method to get text for a node.
065     * See {@link #nodeAt(String)}.
066     *
067     * @param   expression      See {@link #nodeAt(String)}.
068     *
069     * @return  {@link JsonNode#asText()} of {@link #nodeAt(String)}.
070     */
071    protected String textAt(String expression) {
072        JsonNode node = nodeAt(expression);
073
074        return (node != null && (! node.isMissingNode())) ? node.asText() : null;
075    }
076
077    @Override
078    public String toString() {
079        String string = null;
080
081        if (node != null) {
082            try {
083                string = mapper.writeValueAsString(node);
084            } catch (Exception exception) {
085                throw new IllegalStateException(exception);
086            }
087        }
088
089        return (string != null) ? string : super.toString();
090    }
091}