001package com.nimbusds.common.appendable;
002
003
004import jakarta.ws.rs.WebApplicationException;
005import net.minidev.json.JSONAware;
006import net.minidev.json.JSONValue;
007
008import java.io.IOException;
009import java.io.Writer;
010import java.util.function.Consumer;
011
012
013/**
014 * JSON object writer for JAX-RS result streaming.
015 *
016 * <p>Use {@link com.nimbusds.common.json.JSONObjectWriter}.
017 */
018@Deprecated
019public class JSONObjectWriter<T extends JSONAware> implements Consumer<T>, Appendable<T> {
020
021
022        /**
023         * Writer for the appended elements.
024         */
025        private final Writer writer;
026
027
028        /**
029         * The JSON object key extractor.
030         */
031        private final KeyExtractor<T> keyExtractor;
032
033
034        /**
035         * The position of the appender.
036         */
037        private boolean first;
038
039
040        /**
041         * Creates a new JSON object writer.
042         *
043         * @param writer       Writer for the JSON object. Must not be
044         *                     {@code null}.
045         * @param keyExtractor Extracts or determines a JSON object key from
046         *                     the appended elements. Must not be {@code null}.
047         */
048        public JSONObjectWriter(final Writer writer, final KeyExtractor<T> keyExtractor) {
049
050
051                if (writer == null) {
052                        throw new IllegalArgumentException("The writer must not be null");
053                }
054
055                this.writer = writer;
056
057                if (keyExtractor == null) {
058                        throw new IllegalArgumentException("The key extractor must not be null");
059                }
060
061                this.keyExtractor = keyExtractor;
062
063                first = true;
064        }
065
066
067        /**
068         * Writes out the opening '{' of the JSON object.
069         */
070        public void writeStart() {
071
072                try {
073                        writer.write('{');
074
075                } catch (IOException e) {
076
077                        throw new WebApplicationException("Couldn't write JSON object: " + e.getMessage(), e);
078                }
079        }
080
081
082        @Override
083        public void accept(final T element) {
084
085                if (element == null) {
086                        return; // Nothing to append
087                }
088
089                String key = keyExtractor.extractKey(element);
090
091                if (key == null) {
092                        return; // Couldn't extract key
093                }
094
095                StringBuilder sb = new StringBuilder();
096
097                if (first) {
098                        first = false;
099                } else {
100                        sb.append(',');
101                }
102
103                sb.append('"');
104
105                sb.append(JSONValue.escape(key));
106                sb.append('"');
107                sb.append(':');
108                sb.append(element.toJSONString());
109
110                try {
111                        writer.write(sb.toString());
112                        writer.flush();
113
114                } catch (IOException e) {
115
116                        throw new WebApplicationException("Couldn't write JSON object: " + e.getMessage(), e);
117                }
118        }
119        
120        
121        @Override
122        public void append(final T element) {
123                accept(element);
124        }
125
126
127        /**
128         * Writes out the closing '}' of the JSON object and closes the writer.
129         */
130        public void writeEnd() {
131
132                try {
133                        writer.write('}');
134                        writer.close();
135
136                } catch (IOException e) {
137
138                        throw new WebApplicationException("Couldn't write JSON object: " + e.getMessage(), e);
139                }
140        }
141}
142