File: json_ext.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (199 lines) | stat: -rw-r--r-- 5,012 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
   FALCON - The Falcon Programming Language
   FILE: json_ext.cpp

   JSON transport format interface - extension implementation.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Sun, 27 Sep 2009 18:28:44 +0200

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

/** \file
   Funcext module main file - extension implementation.
*/

#include <falcon/setup.h>
#include <falcon/module.h>
#include <falcon/vm.h>
#include <falcon/stream.h>
#include <falcon/stringstream.h>
#include <falcon/rosstream.h>

#include "json_ext.h"
#include "json_mod.h"
#include "json_st.h"



/*#
   @beginmodule feathers.json
*/

namespace Falcon {
namespace Ext {


/*#
   @function JSONencode
   @brief Encode an item in JSON format.
   @param item the item to be encoded in JSON format.
   @optparam stream A stream on which to send the encoded result.
   @optparam pretty Add spacing around separators and puntaction.
   @optparam readable Put each item in lists on a separate line.
   @return a string containing the JSON string, if @b stream is nil
   @raise JSONError if the passed item cannot be turned into a JSON representation.
   @raise IoError in case of error on target stream.

*/

FALCON_FUNC  JSONencode ( ::Falcon::VMachine *vm )
{
   Item *i_item = vm->param(0);
   Item *i_stream = vm->param(1);
   Item *i_pretty = vm->param(2);
   Item *i_readable = vm->param(3);

   Stream* target = 0;
   bool bDel;

   if ( i_item == 0 ||
      (i_stream != 0 && ! i_stream->isNil() && ! i_stream->isOfClass( "Stream" ))
        )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
            origin( e_orig_runtime ).
            extra("X, [Stream]") );
   }

   if ( i_stream == 0 || i_stream->isNil() )
   {
      bDel = true;
      target = new StringStream;
   }
   else {
      bDel = false;
      target = dyncast<Stream*>( i_stream->asObject()->getFalconData() );
   }

   bool bPretty = i_pretty != 0 && i_pretty->isTrue();
   bool bReadable = i_readable != 0 && i_readable->isTrue();

   JSON encoder( bPretty, bReadable );
   bool result =  encoder.encode( *i_item, target );

   if( bDel )
   {
      vm->retval( static_cast<StringStream*>(target)->closeToString() );
      delete target;
   }
   else
   {
      if( ! target->good() )
      {
         throw new IoError(  ErrorParam( e_io_error, __LINE__ ).
            origin( e_orig_runtime ).
            sysError( target->lastError() ) );
      }
   }

   if ( ! result )
   {
      throw new JSONError( ErrorParam( FALCON_JSON_NOT_CODEABLE, __LINE__  )
            .origin( e_orig_runtime )
            .desc( FAL_STR(json_msg_non_codeable) ) );
   }

}

/*#
   @function JSONdecode
   @brief Decode an item stored in JSON format.
   @param source A string or a stream from which to read the JSON data.
   @return a string containing the JSON string, if @b stream is nil
   @raise JSONError if the input data cannot be parsed.
   @raise IoError in case of error on the source stream.

*/

FALCON_FUNC  JSONdecode ( ::Falcon::VMachine *vm )
{
   Item *i_source = vm->param(0);

   Stream* target = 0;
   bool bDel;

   if ( i_source == 0 || ! (i_source->isString() || i_source->isOfClass( "Stream" ))
        )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
            origin( e_orig_runtime ).
            extra("S|Stream") );
   }

   if ( i_source->isString() )
   {
      bDel = true;
      target = new ROStringStream(*i_source->asString());
   }
   else {
      bDel = false;
      target = dyncast<Stream*>( i_source->asObject()->getFalconData() );
   }

   Item item;
   JSON encoder;
   bool result = encoder.decode( item, target );

   // ok also in case of error -- actually better, as it clears garbage
   vm->retval( item );

   if( bDel )
   {
      delete target;
   }
   else if( ! target->good() && ! target->eof() )
   {
      throw new IoError(  ErrorParam( e_io_error, __LINE__ ).
         origin( e_orig_runtime ).
         sysError( target->lastError() ) );
   }

   if ( ! result )
   {
      throw new JSONError( ErrorParam( FALCON_JSON_NOT_DECODABLE, __LINE__  )
            .origin( e_orig_runtime )
            .desc( FAL_STR(json_msg_non_decodable) ) );
   }
}


//=====================================================
// JSON Error
//
/*#
   @class JSONError
   @brief Error generated after error conditions on JSON operations.
   @optparam code The error code
   @optparam desc The description for the error code
   @optparam extra Extra information specifying the error conditions.
   @from Error( code, desc, extra )
*/
FALCON_FUNC  JSONError_init ( ::Falcon::VMachine *vm )
{
   CoreObject *einst = vm->self().asObject();
   if( einst->getUserData() == 0 )
      einst->setUserData( new JSONError );

   ::Falcon::core::Error_init( vm );
}

}
}


/* end of funcext_ext.cpp */