blob: dc1535b995ed09305f19a4dfb73c7d9ac48c3bf1 [file] [log] [blame]
Tatu Saloranta68bb83d2013-04-19 10:41:15 -07001/* Jackson JSON-processor.
2 *
3 * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi
4 */
5
Tatu Salorantaf15531c2011-12-22 23:00:40 -08006package com.fasterxml.jackson.core;
7
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -08008import java.io.IOException;
9import java.io.OutputStream;
10import java.nio.ByteBuffer;
11
Tatu Salorantaf15531c2011-12-22 23:00:40 -080012/**
13 * Interface that defines how Jackson package can interact with efficient
14 * pre-serialized or lazily-serialized and reused String representations.
15 * Typically implementations store possible serialized version(s) so that
16 * serialization of String can be done more efficiently, especially when
17 * used multiple times.
Tatu Saloranta066463a2013-09-22 19:57:25 -070018 *<p>
19 * Note that "quoted" in methods means quoting of 'special' characters using
20 * JSON backlash notation (and not use of actual double quotes).
Tatu Salorantaf15531c2011-12-22 23:00:40 -080021 *
22 * @see com.fasterxml.jackson.core.io.SerializedString
23 */
24public interface SerializableString
25{
26 /**
27 * Returns unquoted String that this object represents (and offers
28 * serialized forms for)
29 */
Francis Galiegue4d5def12012-09-29 12:26:42 +020030 String getValue();
Tatu Salorantaf15531c2011-12-22 23:00:40 -080031
32 /**
33 * Returns length of the (unquoted) String as characters.
34 * Functionally equvalent to:
35 *<pre>
36 * getValue().length();
37 *</pre>
38 */
Francis Galiegue4d5def12012-09-29 12:26:42 +020039 int charLength();
Tatu Salorantaf15531c2011-12-22 23:00:40 -080040
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -080041 /*
42 /**********************************************************
43 /* Accessors for byte sequences
44 /**********************************************************
45 */
46
Tatu Salorantaf15531c2011-12-22 23:00:40 -080047 /**
Tatu Saloranta066463a2013-09-22 19:57:25 -070048 * Returns JSON quoted form of the String, as character array.
49 * Result can be embedded as-is in textual JSON as property name or JSON String.
Tatu Salorantaf15531c2011-12-22 23:00:40 -080050 */
Francis Galiegue4d5def12012-09-29 12:26:42 +020051 char[] asQuotedChars();
Tatu Salorantaf15531c2011-12-22 23:00:40 -080052
53 /**
54 * Returns UTF-8 encoded version of unquoted String.
55 * Functionally equivalent to (but more efficient than):
56 *<pre>
57 * getValue().getBytes("UTF-8");
58 *</pre>
59 */
Francis Galiegue4d5def12012-09-29 12:26:42 +020060 byte[] asUnquotedUTF8();
Tatu Salorantaf15531c2011-12-22 23:00:40 -080061
62 /**
63 * Returns UTF-8 encoded version of JSON-quoted String.
64 * Functionally equivalent to (but more efficient than):
65 *<pre>
66 * new String(asQuotedChars()).getBytes("UTF-8");
67 *</pre>
68 */
Francis Galiegue4d5def12012-09-29 12:26:42 +020069 byte[] asQuotedUTF8();
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -080070
71 /*
72 /**********************************************************
73 /* Helper methods for appending byte/char sequences
74 /**********************************************************
75 */
76
77 /**
78 * Method that will append quoted UTF-8 bytes of this String into given
79 * buffer, if there is enough room; if not, returns -1.
80 * Functionally equivalent to:
81 *<pre>
82 * byte[] bytes = str.asQuotedUTF8();
83 * System.arraycopy(bytes, 0, buffer, offset, bytes.length);
84 * return bytes.length;
85 *</pre>
86 *
87 * @return Number of bytes appended, if successful, otherwise -1
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -080088 */
Francis Galiegue4d5def12012-09-29 12:26:42 +020089 int appendQuotedUTF8(byte[] buffer, int offset);
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -080090
91 /**
92 * Method that will append quoted characters of this String into given
93 * buffer. Functionally equivalent to:
94 *<pre>
95 * char[] ch = str.asQuotedChars();
96 * System.arraycopy(ch, 0, buffer, offset, ch.length);
97 * return ch.length;
98 *</pre>
99 *
100 * @return Number of characters appended, if successful, otherwise -1
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800101 */
Francis Galiegue4d5def12012-09-29 12:26:42 +0200102 int appendQuoted(char[] buffer, int offset);
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800103
104 /**
105 * Method that will append unquoted ('raw') UTF-8 bytes of this String into given
106 * buffer. Functionally equivalent to:
107 *<pre>
108 * byte[] bytes = str.asUnquotedUTF8();
109 * System.arraycopy(bytes, 0, buffer, offset, bytes.length);
110 * return bytes.length;
111 *</pre>
112 *
113 * @return Number of bytes appended, if successful, otherwise -1
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800114 */
Francis Galiegue4d5def12012-09-29 12:26:42 +0200115 int appendUnquotedUTF8(byte[] buffer, int offset);
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800116
117
118 /**
119 * Method that will append unquoted characters of this String into given
120 * buffer. Functionally equivalent to:
121 *<pre>
122 * char[] ch = str.getValue().toCharArray();
123 * System.arraycopy(bytes, 0, buffer, offset, ch.length);
124 * return ch.length;
125 *</pre>
126 *
127 * @return Number of characters appended, if successful, otherwise -1
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800128 */
Francis Galiegue4d5def12012-09-29 12:26:42 +0200129 int appendUnquoted(char[] buffer, int offset);
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800130
131 /*
132 /**********************************************************
133 /* Helper methods for writing out byte sequences
134 /**********************************************************
135 */
136
137 /**
138 * @return Number of bytes written
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800139 */
Francis Galiegue4d5def12012-09-29 12:26:42 +0200140 int writeQuotedUTF8(OutputStream out) throws IOException;
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800141
142 /**
143 * @return Number of bytes written
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800144 */
Francis Galiegue4d5def12012-09-29 12:26:42 +0200145 int writeUnquotedUTF8(OutputStream out) throws IOException;
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800146
147 /**
148 * @return Number of bytes put, if successful, otherwise -1
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800149 */
Francis Galiegue4d5def12012-09-29 12:26:42 +0200150 int putQuotedUTF8(ByteBuffer buffer) throws IOException;
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800151
152 /**
153 * @return Number of bytes put, if successful, otherwise -1
Tatu Saloranta0ccc4c92012-01-17 22:13:16 -0800154 */
Francis Galiegue4d5def12012-09-29 12:26:42 +0200155 int putUnquotedUTF8(ByteBuffer out) throws IOException;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800156}