blob: aa4aed89304196db90a6e5eacad17eaa53e96bcf [file] [log] [blame]
Anthony F. Molinaroa6530672011-09-18 04:57:50 +00001%%
2%% Licensed to the Apache Software Foundation (ASF) under one
3%% or more contributor license agreements. See the NOTICE file
4%% distributed with this work for additional information
5%% regarding copyright ownership. The ASF licenses this file
6%% to you under the Apache License, Version 2.0 (the
7%% "License"); you may not use this file except in compliance
8%% with the License. You may obtain a copy of the License at
9%%
10%% http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing,
13%% software distributed under the License is distributed on an
14%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15%% KIND, either express or implied. See the License for the
16%% specific language governing permissions and limitations
17%% under the License.
18%%
19%% The JSON protocol implementation was created by
20%% Peter Neumark <neumark.peter@gmail.com> based on
21%% the binary protocol implementation.
22
23-module(thrift_json_protocol).
24
25-behaviour(thrift_protocol).
26
27-include("thrift_constants.hrl").
28-include("thrift_protocol.hrl").
29
30-export([new/1, new/2,
31 read/2,
32 write/2,
33 flush_transport/1,
34 close_transport/1,
35 new_protocol_factory/2
36 ]).
37
38-record(json_context, {
39 % the type of json_context: array or object
40 type,
41 % fields read or written
42 fields_processed = 0
43}).
44
45-record(json_protocol, {
46 transport,
47 context_stack = [],
48 jsx
49}).
50-type state() :: #json_protocol{}.
51-include("thrift_protocol_behaviour.hrl").
52
53-define(VERSION_1, 1).
54-define(JSON_DOUBLE_PRECISION, 16).
55
56typeid_to_json(?tType_BOOL) -> "tf";
57typeid_to_json(?tType_BYTE) -> "i8";
58typeid_to_json(?tType_DOUBLE) -> "dbl";
59typeid_to_json(?tType_I16) -> "i16";
60typeid_to_json(?tType_I32) -> "i32";
61typeid_to_json(?tType_I64) -> "i64";
62typeid_to_json(?tType_STRING) -> "str";
63typeid_to_json(?tType_STRUCT) -> "rec";
64typeid_to_json(?tType_MAP) -> "map";
65typeid_to_json(?tType_SET) -> "set";
66typeid_to_json(?tType_LIST) -> "lst".
67
68json_to_typeid("tf") -> ?tType_BOOL;
69json_to_typeid("i8") -> ?tType_BYTE;
70json_to_typeid("dbl") -> ?tType_DOUBLE;
71json_to_typeid("i16") -> ?tType_I16;
72json_to_typeid("i32") -> ?tType_I32;
73json_to_typeid("i64") -> ?tType_I64;
74json_to_typeid("str") -> ?tType_STRING;
75json_to_typeid("rec") -> ?tType_STRUCT;
76json_to_typeid("map") -> ?tType_MAP;
77json_to_typeid("set") -> ?tType_SET;
78json_to_typeid("lst") -> ?tType_LIST.
79
80start_context(object) -> "{";
81start_context(array) -> "[".
82
83end_context(object) -> "}";
84end_context(array) -> "]".
85
86
87new(Transport) ->
88 new(Transport, _Options = []).
89
90new(Transport, _Options) ->
91 State = #json_protocol{transport = Transport},
92 thrift_protocol:new(?MODULE, State).
93
94flush_transport(This = #json_protocol{transport = Transport}) ->
95 {NewTransport, Result} = thrift_transport:flush(Transport),
96 {This#json_protocol{
97 transport = NewTransport,
98 context_stack = []
99 }, Result}.
100
101close_transport(This = #json_protocol{transport = Transport}) ->
102 {NewTransport, Result} = thrift_transport:close(Transport),
103 {This#json_protocol{
104 transport = NewTransport,
105 context_stack = [],
106 jsx = undefined
107 }, Result}.
108
109%%%
110%%% instance methods
111%%%
112% places a new context on the stack:
113write(#json_protocol{context_stack = Stack} = State0, {enter_context, Type}) ->
114 {State1, ok} = write_values(State0, [{context_pre_item, false}]),
115 State2 = State1#json_protocol{context_stack = [
116 #json_context{type=Type}|Stack]},
117 write_values(State2, [list_to_binary(start_context(Type))]);
118
119% removes the topmost context from stack
120write(#json_protocol{context_stack = [CurrCtxt|Stack]} = State0, {exit_context}) ->
121 Type = CurrCtxt#json_context.type,
122 State1 = State0#json_protocol{context_stack = Stack},
123 write_values(State1, [
124 list_to_binary(end_context(Type)),
125 {context_post_item, false}
126 ]);
127
128% writes necessary prelude to field or container depending on current context
129write(#json_protocol{context_stack = []} = This0,
130 {context_pre_item, _}) -> {This0, ok};
131write(#json_protocol{context_stack = [Context|_CtxtTail]} = This0,
132 {context_pre_item, MayNeedQuotes}) ->
133 FieldNo = Context#json_context.fields_processed,
134 CtxtType = Context#json_context.type,
135 Rem = FieldNo rem 2,
136 case {CtxtType, FieldNo, Rem, MayNeedQuotes} of
137 {array, N, _, _} when N > 0 -> % array element (not first)
138 write(This0, <<",">>);
139 {object, 0, _, true} -> % non-string object key (first)
140 write(This0, <<"\"">>);
141 {object, N, 0, true} when N > 0 -> % non-string object key (not first)
142 write(This0, <<",\"">>);
143 {object, N, 0, false} when N > 0-> % string object key (not first)
144 write(This0, <<",">>);
145 _ -> % no pre-field necessary
146 {This0, ok}
147 end;
148
149% writes necessary postlude to field or container depending on current context
150write(#json_protocol{context_stack = []} = This0,
151 {context_post_item, _}) -> {This0, ok};
152write(#json_protocol{context_stack = [Context|CtxtTail]} = This0,
153 {context_post_item, MayNeedQuotes}) ->
154 FieldNo = Context#json_context.fields_processed,
155 CtxtType = Context#json_context.type,
156 Rem = FieldNo rem 2,
157 {This1, ok} = case {CtxtType, Rem, MayNeedQuotes} of
158 {object, 0, true} -> % non-string object key
159 write(This0, <<"\":">>);
160 {object, 0, false} -> % string object key
161 write(This0, <<":">>);
162 _ -> % no pre-field necessary
163 {This0, ok}
164 end,
165 NewContext = Context#json_context{fields_processed = FieldNo + 1},
166 {This1#json_protocol{context_stack=[NewContext|CtxtTail]}, ok};
167
168write(This0, #protocol_message_begin{
169 name = Name,
170 type = Type,
171 seqid = Seqid}) ->
172 write_values(This0, [
173 {enter_context, array},
174 {i32, ?VERSION_1},
175 {string, Name},
176 {i32, Type},
177 {i32, Seqid}
178 ]);
179
180write(This, message_end) ->
181 write_values(This, [{exit_context}]);
182
183% Example field expression: "1":{"dbl":3.14}
184write(This0, #protocol_field_begin{
185 name = _Name,
186 type = Type,
187 id = Id}) ->
188 write_values(This0, [
189 % entering 'outer' object
190 {i16, Id},
191 % entering 'outer' object
192 {enter_context, object},
193 {string, typeid_to_json(Type)}
194 ]);
195
196write(This, field_stop) ->
197 {This, ok};
198
199write(This, field_end) ->
200 write_values(This,[{exit_context}]);
201
202% Example message with map: [1,"testMap",1,0,{"1":{"map":["i32","i32",3,{"7":77,"8":88,"9":99}]}}]
203write(This0, #protocol_map_begin{
204 ktype = Ktype,
205 vtype = Vtype,
206 size = Size}) ->
207 write_values(This0, [
208 {enter_context, array},
209 {string, typeid_to_json(Ktype)},
210 {string, typeid_to_json(Vtype)},
211 {i32, Size},
212 {enter_context, object}
213 ]);
214
215write(This, map_end) ->
216 write_values(This,[
217 {exit_context},
218 {exit_context}
219 ]);
220
221write(This0, #protocol_list_begin{
222 etype = Etype,
223 size = Size}) ->
224 write_values(This0, [
225 {enter_context, array},
226 {string, typeid_to_json(Etype)},
227 {i32, Size}
228 ]);
229
230write(This, list_end) ->
231 write_values(This,[
232 {exit_context}
233 ]);
234
235% example message with set: [1,"testSet",1,0,{"1":{"set":["i32",3,1,2,3]}}]
236write(This0, #protocol_set_begin{
237 etype = Etype,
238 size = Size}) ->
239 write_values(This0, [
240 {enter_context, array},
241 {string, typeid_to_json(Etype)},
242 {i32, Size}
243 ]);
244
245write(This, set_end) ->
246 write_values(This,[
247 {exit_context}
248 ]);
249% example message with struct: [1,"testStruct",1,0,{"1":{"rec":{"1":{"str":"worked"},"4":{"i8":1},"9":{"i32":1073741824},"11":{"i64":1152921504606847000}}}}]
250write(This, #protocol_struct_begin{}) ->
251 write_values(This, [
252 {enter_context, object}
253 ]);
254
255write(This, struct_end) ->
256 write_values(This,[
257 {exit_context}
258 ]);
259
260write(This, {bool, true}) -> write_values(This, [
261 {context_pre_item, true},
262 <<"true">>,
263 {context_post_item, true}
264 ]);
265
266write(This, {bool, false}) -> write_values(This, [
267 {context_pre_item, true},
268 <<"false">>,
269 {context_post_item, true}
270 ]);
271
272write(This, {byte, Byte}) -> write_values(This, [
273 {context_pre_item, true},
274 list_to_binary(integer_to_list(Byte)),
275 {context_post_item, true}
276 ]);
277
278write(This, {i16, I16}) ->
279 write(This, {byte, I16});
280
281write(This, {i32, I32}) ->
282 write(This, {byte, I32});
283
284write(This, {i64, I64}) ->
285 write(This, {byte, I64});
286
287write(This, {double, Double}) -> write_values(This, [
288 {context_pre_item, true},
289 list_to_binary(io_lib:format("~.*f", [?JSON_DOUBLE_PRECISION,Double])),
290 {context_post_item, true}
291 ]);
292
293write(This0, {string, Str}) -> write_values(This0, [
294 {context_pre_item, false},
295 case is_binary(Str) of
296 true -> Str;
Jake Farrell7dcd49f2011-09-26 15:29:03 +0000297 false -> jsx:term_to_json(list_to_binary(Str), [{strict, false}])
Anthony F. Molinaroa6530672011-09-18 04:57:50 +0000298 end,
299 {context_post_item, false}
300 ]);
301
302%% TODO: binary fields should be base64 encoded?
303
304%% Data :: iolist()
305write(This = #json_protocol{transport = Trans}, Data) ->
306 %io:format("Data ~p Ctxt ~p~n~n", [Data, This#json_protocol.context_stack]),
307 {NewTransport, Result} = thrift_transport:write(Trans, Data),
308 {This#json_protocol{transport = NewTransport}, Result}.
309
310write_values(This0, ValueList) ->
311 FinalState = lists:foldl(
312 fun(Val, ThisIn) ->
313 {ThisOut, ok} = write(ThisIn, Val),
314 ThisOut
315 end,
316 This0,
317 ValueList),
318 {FinalState, ok}.
319
320%% I wish the erlang version of the transport interface included a
321%% read_all function (like eg. the java implementation). Since it doesn't,
322%% here's my version (even though it probably shouldn't be in this file).
323%%
324%% The resulting binary is immediately send to the JSX stream parser.
325%% Subsequent calls to read actually operate on the events returned by JSX.
326read_all(#json_protocol{transport = Transport0} = State) ->
327 {Transport1, Bin} = read_all_1(Transport0, []),
Jake Farrell7dcd49f2011-09-26 15:29:03 +0000328 P = jsx:parser(),
Anthony F. Molinaroa6530672011-09-18 04:57:50 +0000329 State#json_protocol{
330 transport = Transport1,
331 jsx = P(Bin)
332 }.
333
334read_all_1(Transport0, IoList) ->
335 {Transport1, Result} = thrift_transport:read(Transport0, 1),
336 case Result of
337 {ok, <<>>} -> % nothing read: assume we're done
338 {Transport1, iolist_to_binary(lists:reverse(IoList))};
339 {ok, Data} -> % character successfully read; read more
340 read_all_1(Transport1, [Data|IoList]);
341 {error, 'EOF'} -> % we're done
342 {Transport1, iolist_to_binary(lists:reverse(IoList))}
343 end.
344
345% Expect reads an event from the JSX event stream. It receives an event or data
346% type as input. Comparing the read event from the one is was passed, it
347% returns an error if something other than the expected value is encountered.
348% Expect also maintains the context stack in #json_protocol.
349expect(#json_protocol{jsx={event, {Type, Data}=Ev, Next}}=State, ExpectedType) ->
350 NextState = State#json_protocol{jsx=Next()},
351 case Type == ExpectedType of
352 true ->
353 {NextState, {ok, convert_data(Type, Data)}};
354 false ->
355 {NextState, {error, {unexpected_json_event, Ev}}}
356 end;
357
358expect(#json_protocol{jsx={event, Event, Next}}=State, ExpectedEvent) ->
359 expect(State#json_protocol{jsx={event, {Event, none}, Next}}, ExpectedEvent).
360
361convert_data(integer, I) -> list_to_integer(I);
362convert_data(float, F) -> list_to_float(F);
363convert_data(_, D) -> D.
364
365expect_many(State, ExpectedList) ->
366 expect_many_1(State, ExpectedList, [], ok).
367
368expect_many_1(State, [], ResultList, Status) ->
369 {State, {Status, lists:reverse(ResultList)}};
370expect_many_1(State, [Expected|ExpTail], ResultList, _PrevStatus) ->
371 {State1, {Status, Data}} = expect(State, Expected),
372 NewResultList = [Data|ResultList],
373 case Status of
374 % in case of error, end prematurely
375 error -> expect_many_1(State1, [], NewResultList, Status);
376 ok -> expect_many_1(State1, ExpTail, NewResultList, Status)
377 end.
378
379% wrapper around expect to make life easier for container opening/closing functions
380expect_nodata(This, ExpectedList) ->
381 case expect_many(This, ExpectedList) of
382 {State, {ok, _}} ->
383 {State, ok};
384 Error ->
385 Error
386 end.
387
388read_field(#json_protocol{jsx={event, Field, Next}} = State) ->
389 NewState = State#json_protocol{jsx=Next()},
390 {NewState, Field}.
391
392read(This0, message_begin) ->
393 % call read_all to get the contents of the transport buffer into JSX.
394 This1 = read_all(This0),
395 case expect_many(This1,
396 [start_array, integer, string, integer, integer]) of
397 {This2, {ok, [_, Version, Name, Type, SeqId]}} ->
398 case Version =:= ?VERSION_1 of
399 true ->
400 {This2, #protocol_message_begin{name = Name,
401 type = Type,
402 seqid = SeqId}};
403 false ->
404 {This2, {error, no_json_protocol_version}}
405 end;
406 Other -> Other
407 end;
408
409read(This, message_end) ->
410 expect_nodata(This, [end_array]);
411
412read(This, struct_begin) ->
413 expect_nodata(This, [start_object]);
414
415read(This, struct_end) ->
416 expect_nodata(This, [end_object]);
417
418read(This0, field_begin) ->
419 {This1, Read} = expect_many(This0,
420 [%field id
421 key,
422 % {} surrounding field
423 start_object,
424 % type of field
425 key]),
426 case Read of
427 {ok, [FieldIdStr, _, FieldType]} ->
428 {This1, #protocol_field_begin{
429 type = json_to_typeid(FieldType),
430 id = list_to_integer(FieldIdStr)}}; % TODO: do we need to wrap this in a try/catch?
431 {error,[{unexpected_json_event, {end_object,none}}]} ->
432 {This1, #protocol_field_begin{type = ?tType_STOP}};
433 Other ->
434 io:format("**** OTHER branch selected ****"),
435 {This1, Other}
436 end;
437
438read(This, field_end) ->
439 expect_nodata(This, [end_object]);
440
441% Example message with map: [1,"testMap",1,0,{"1":{"map":["i32","i32",3,{"7":77,"8":88,"9":99}]}}]
442read(This0, map_begin) ->
443 case expect_many(This0,
444 [start_array,
445 % key type
446 string,
447 % value type
448 string,
449 % size
450 integer,
451 % the following object contains the map
452 start_object]) of
453 {This1, {ok, [_, Ktype, Vtype, Size, _]}} ->
454 {This1, #protocol_map_begin{ktype = Ktype,
455 vtype = Vtype,
456 size = Size}};
457 Other -> Other
458 end;
459
460read(This, map_end) ->
461 expect_nodata(This, [end_object, end_array]);
462
463read(This0, list_begin) ->
464 case expect_many(This0,
465 [start_array,
466 % element type
467 string,
468 % size
469 integer]) of
470 {This1, {ok, [_, Etype, Size]}} ->
471 {This1, #protocol_list_begin{
472 etype = Etype,
473 size = Size}};
474 Other -> Other
475 end;
476
477read(This, list_end) ->
478 expect_nodata(This, [end_array]);
479
480% example message with set: [1,"testSet",1,0,{"1":{"set":["i32",3,1,2,3]}}]
481read(This0, set_begin) ->
482 case expect_many(This0,
483 [start_array,
484 % element type
485 string,
486 % size
487 integer]) of
488 {This1, {ok, [_, Etype, Size]}} ->
489 {This1, #protocol_set_begin{
490 etype = Etype,
491 size = Size}};
492 Other -> Other
493 end;
494
495read(This, set_end) ->
496 expect_nodata(This, [end_array]);
497
498read(This0, field_stop) ->
499 {This0, ok};
500%%
501
502read(This0, bool) ->
503 {This1, Field} = read_field(This0),
504 Value = case Field of
505 {literal, I} ->
506 {ok, I};
507 _Other ->
508 {error, unexpected_event_for_boolean}
509 end,
510 {This1, Value};
511
512read(This0, byte) ->
513 {This1, Field} = read_field(This0),
514 Value = case Field of
515 {key, K} ->
516 {ok, list_to_integer(K)};
517 {integer, I} ->
518 {ok, list_to_integer(I)};
519 _Other ->
520 {error, unexpected_event_for_integer}
521 end,
522 {This1, Value};
523
524read(This0, i16) ->
525 read(This0, byte);
526
527read(This0, i32) ->
528 read(This0, byte);
529
530read(This0, i64) ->
531 read(This0, byte);
532
533read(This0, double) ->
534 {This1, Field} = read_field(This0),
535 Value = case Field of
536 {float, I} ->
537 {ok, list_to_float(I)};
538 _Other ->
539 {error, unexpected_event_for_double}
540 end,
541 {This1, Value};
542
543% returns a binary directly, call binary_to_list if necessary
544read(This0, string) ->
545 {This1, Field} = read_field(This0),
546 Value = case Field of
547 {string, I} ->
548 {ok, I};
549 {key, J} ->
550 {ok, J};
551 _Other ->
552 {error, unexpected_event_for_string}
553 end,
554 {This1, Value}.
555
556%%%% FACTORY GENERATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557
558%% returns a (fun() -> thrift_protocol())
559new_protocol_factory(TransportFactory, _Options) ->
560 % Only strice read/write are implemented
561 F = fun() ->
562 {ok, Transport} = TransportFactory(),
563 thrift_json_protocol:new(Transport, [])
564 end,
565 {ok, F}.
566