3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "serialize.h"
22 #include "../exceptions.h"
27 // Creates a string with the length as the first two bytes
28 std::string serializeString(const std::string &plain)
30 //assert(plain.size() <= 65535);
31 if(plain.size() > 65535)
32 throw SerializationError("String too long for serializeString");
34 writeU16((u8*)&buf[0], plain.size());
41 // Creates a string with the length as the first two bytes from wide string
42 std::string serializeWideString(const std::wstring &plain)
44 //assert(plain.size() <= 65535);
45 if(plain.size() > 65535)
46 throw SerializationError("String too long for serializeString");
48 writeU16((u8*)buf, plain.size());
51 for(u32 i=0; i<plain.size(); i++)
53 writeU16((u8*)buf, plain[i]);
59 // Reads a string with the length as the first two bytes
60 std::string deSerializeString(std::istream &is)
65 throw SerializationError("deSerializeString: size not read");
66 u16 s_size = readU16((u8*)buf);
69 Buffer<char> buf2(s_size);
70 is.read(&buf2[0], s_size);
73 s.append(&buf2[0], s_size);
77 // Reads a wide string with the length as the first two bytes
78 std::wstring deSerializeWideString(std::istream &is)
83 throw SerializationError("deSerializeString: size not read");
84 u16 s_size = readU16((u8*)buf);
89 for(u32 i=0; i<s_size; i++)
92 wchar_t c16 = readU16((u8*)buf);
98 // Creates a string with the length as the first four bytes
99 std::string serializeLongString(const std::string &plain)
102 writeU32((u8*)&buf[0], plain.size());
109 // Reads a string with the length as the first four bytes
110 std::string deSerializeLongString(std::istream &is)
115 throw SerializationError("deSerializeLongString: size not read");
116 u32 s_size = readU32((u8*)buf);
119 Buffer<char> buf2(s_size);
120 is.read(&buf2[0], s_size);
123 s.append(&buf2[0], s_size);
127 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
128 std::string serializeJsonString(const std::string &plain)
130 std::ostringstream os(std::ios::binary);
132 for(size_t i = 0; i < plain.size(); i++)
137 case '"': os<<"\\\""; break;
138 case '\\': os<<"\\\\"; break;
139 case '/': os<<"\\/"; break;
140 case '\b': os<<"\\b"; break;
141 case '\f': os<<"\\f"; break;
142 case '\n': os<<"\\n"; break;
143 case '\r': os<<"\\r"; break;
144 case '\t': os<<"\\t"; break;
147 if(c >= 32 && c <= 126)
153 u32 cnum = (u32) (u8) c;
154 os<<"\\u"<<std::hex<<std::setw(4)<<std::setfill('0')<<cnum;
164 // Reads a string encoded in JSON format
165 std::string deSerializeJsonString(std::istream &is)
167 std::ostringstream os(std::ios::binary);
170 // Parse initial doublequote
173 throw SerializationError("JSON string must start with doublequote");
180 throw SerializationError("JSON string ended prematurely");
189 throw SerializationError("JSON string ended prematurely");
192 default: os<<c2; break;
193 case 'b': os<<'\b'; break;
194 case 'f': os<<'\f'; break;
195 case 'n': os<<'\n'; break;
196 case 'r': os<<'\r'; break;
197 case 't': os<<'\t'; break;
201 is.read(hexdigits, 4);
203 throw SerializationError("JSON string ended prematurely");
205 std::istringstream tmp_is(hexdigits, std::ios::binary);
207 tmp_is >> std::hex >> hexnumber;
208 os<<((char)hexnumber);