PageRenderTime 72ms CodeModel.GetById 39ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/19/json/pure/generator.rb

http://github.com/rubinius/rubinius
Ruby | 468 lines | 336 code | 42 blank | 90 comment | 30 complexity | 58f0e85ce9e3fb02846d60705bf3d03b MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, 0BSD, GPL-2.0, LGPL-2.1
  1. module JSON
  2. MAP = {
  3. "\x0" => '\u0000',
  4. "\x1" => '\u0001',
  5. "\x2" => '\u0002',
  6. "\x3" => '\u0003',
  7. "\x4" => '\u0004',
  8. "\x5" => '\u0005',
  9. "\x6" => '\u0006',
  10. "\x7" => '\u0007',
  11. "\b" => '\b',
  12. "\t" => '\t',
  13. "\n" => '\n',
  14. "\xb" => '\u000b',
  15. "\f" => '\f',
  16. "\r" => '\r',
  17. "\xe" => '\u000e',
  18. "\xf" => '\u000f',
  19. "\x10" => '\u0010',
  20. "\x11" => '\u0011',
  21. "\x12" => '\u0012',
  22. "\x13" => '\u0013',
  23. "\x14" => '\u0014',
  24. "\x15" => '\u0015',
  25. "\x16" => '\u0016',
  26. "\x17" => '\u0017',
  27. "\x18" => '\u0018',
  28. "\x19" => '\u0019',
  29. "\x1a" => '\u001a',
  30. "\x1b" => '\u001b',
  31. "\x1c" => '\u001c',
  32. "\x1d" => '\u001d',
  33. "\x1e" => '\u001e',
  34. "\x1f" => '\u001f',
  35. '"' => '\"',
  36. '\\' => '\\\\',
  37. } # :nodoc:
  38. # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
  39. # UTF16 big endian characters as \u????, and return it.
  40. if defined?(::Encoding)
  41. def utf8_to_json(string) # :nodoc:
  42. string = string.dup
  43. string << '' # XXX workaround: avoid buffer sharing
  44. string.force_encoding(::Encoding::ASCII_8BIT)
  45. string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
  46. string.force_encoding(::Encoding::UTF_8)
  47. string
  48. end
  49. def utf8_to_json_ascii(string) # :nodoc:
  50. string = string.dup
  51. string << '' # XXX workaround: avoid buffer sharing
  52. string.force_encoding(::Encoding::ASCII_8BIT)
  53. string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
  54. string.gsub!(/(
  55. (?:
  56. [\xc2-\xdf][\x80-\xbf] |
  57. [\xe0-\xef][\x80-\xbf]{2} |
  58. [\xf0-\xf4][\x80-\xbf]{3}
  59. )+ |
  60. [\x80-\xc1\xf5-\xff] # invalid
  61. )/nx) { |c|
  62. c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
  63. s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
  64. s.gsub!(/.{4}/n, '\\\\u\&')
  65. }
  66. string.force_encoding(::Encoding::UTF_8)
  67. string
  68. rescue => e
  69. raise GeneratorError, "Caught #{e.class}: #{e}"
  70. end
  71. else
  72. def utf8_to_json(string) # :nodoc:
  73. string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
  74. end
  75. def utf8_to_json_ascii(string) # :nodoc:
  76. string = string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
  77. string.gsub!(/(
  78. (?:
  79. [\xc2-\xdf][\x80-\xbf] |
  80. [\xe0-\xef][\x80-\xbf]{2} |
  81. [\xf0-\xf4][\x80-\xbf]{3}
  82. )+ |
  83. [\x80-\xc1\xf5-\xff] # invalid
  84. )/nx) { |c|
  85. c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
  86. s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
  87. s.gsub!(/.{4}/n, '\\\\u\&')
  88. }
  89. string
  90. rescue => e
  91. raise GeneratorError, "Caught #{e.class}: #{e}"
  92. end
  93. end
  94. module_function :utf8_to_json, :utf8_to_json_ascii
  95. module Pure
  96. module Generator
  97. # This class is used to create State instances, that are use to hold data
  98. # while generating a JSON text from a Ruby data structure.
  99. class State
  100. # Creates a State object from _opts_, which ought to be Hash to create
  101. # a new State instance configured by _opts_, something else to create
  102. # an unconfigured instance. If _opts_ is a State object, it is just
  103. # returned.
  104. def self.from_state(opts)
  105. case
  106. when self === opts
  107. opts
  108. when opts.respond_to?(:to_hash)
  109. new(opts.to_hash)
  110. when opts.respond_to?(:to_h)
  111. new(opts.to_h)
  112. else
  113. SAFE_STATE_PROTOTYPE.dup
  114. end
  115. end
  116. # Instantiates a new State object, configured by _opts_.
  117. #
  118. # _opts_ can have the following keys:
  119. #
  120. # * *indent*: a string used to indent levels (default: ''),
  121. # * *space*: a string that is put after, a : or , delimiter (default: ''),
  122. # * *space_before*: a string that is put before a : pair delimiter (default: ''),
  123. # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
  124. # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
  125. # * *check_circular*: is deprecated now, use the :max_nesting option instead,
  126. # * *max_nesting*: sets the maximum level of data structure nesting in
  127. # the generated JSON, max_nesting = 0 if no maximum should be checked.
  128. # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
  129. # generated, otherwise an exception is thrown, if these values are
  130. # encountered. This options defaults to false.
  131. # * *quirks_mode*: Enables quirks_mode for parser, that is for example
  132. # generating single JSON values instead of documents is possible.
  133. def initialize(opts = {})
  134. @indent = ''
  135. @space = ''
  136. @space_before = ''
  137. @object_nl = ''
  138. @array_nl = ''
  139. @allow_nan = false
  140. @ascii_only = false
  141. @quirks_mode = false
  142. @buffer_initial_length = 1024
  143. configure opts
  144. end
  145. # This string is used to indent levels in the JSON text.
  146. attr_accessor :indent
  147. # This string is used to insert a space between the tokens in a JSON
  148. # string.
  149. attr_accessor :space
  150. # This string is used to insert a space before the ':' in JSON objects.
  151. attr_accessor :space_before
  152. # This string is put at the end of a line that holds a JSON object (or
  153. # Hash).
  154. attr_accessor :object_nl
  155. # This string is put at the end of a line that holds a JSON array.
  156. attr_accessor :array_nl
  157. # This integer returns the maximum level of data structure nesting in
  158. # the generated JSON, max_nesting = 0 if no maximum is checked.
  159. attr_accessor :max_nesting
  160. # If this attribute is set to true, quirks mode is enabled, otherwise
  161. # it's disabled.
  162. attr_accessor :quirks_mode
  163. # :stopdoc:
  164. attr_reader :buffer_initial_length
  165. def buffer_initial_length=(length)
  166. if length > 0
  167. @buffer_initial_length = length
  168. end
  169. end
  170. # :startdoc:
  171. # This integer returns the current depth data structure nesting in the
  172. # generated JSON.
  173. attr_accessor :depth
  174. def check_max_nesting # :nodoc:
  175. return if @max_nesting.zero?
  176. current_nesting = depth + 1
  177. current_nesting > @max_nesting and
  178. raise NestingError, "nesting of #{current_nesting} is too deep"
  179. end
  180. # Returns true, if circular data structures are checked,
  181. # otherwise returns false.
  182. def check_circular?
  183. !@max_nesting.zero?
  184. end
  185. # Returns true if NaN, Infinity, and -Infinity should be considered as
  186. # valid JSON and output.
  187. def allow_nan?
  188. @allow_nan
  189. end
  190. # Returns true, if only ASCII characters should be generated. Otherwise
  191. # returns false.
  192. def ascii_only?
  193. @ascii_only
  194. end
  195. # Returns true, if quirks mode is enabled. Otherwise returns false.
  196. def quirks_mode?
  197. @quirks_mode
  198. end
  199. # Configure this State instance with the Hash _opts_, and return
  200. # itself.
  201. def configure(opts)
  202. @indent = opts[:indent] if opts.key?(:indent)
  203. @space = opts[:space] if opts.key?(:space)
  204. @space_before = opts[:space_before] if opts.key?(:space_before)
  205. @object_nl = opts[:object_nl] if opts.key?(:object_nl)
  206. @array_nl = opts[:array_nl] if opts.key?(:array_nl)
  207. @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
  208. @ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
  209. @depth = opts[:depth] || 0
  210. @quirks_mode = opts[:quirks_mode] if opts.key?(:quirks_mode)
  211. if !opts.key?(:max_nesting) # defaults to 19
  212. @max_nesting = 19
  213. elsif opts[:max_nesting]
  214. @max_nesting = opts[:max_nesting]
  215. else
  216. @max_nesting = 0
  217. end
  218. self
  219. end
  220. alias merge configure
  221. # Returns the configuration instance variables as a hash, that can be
  222. # passed to the configure method.
  223. def to_h
  224. result = {}
  225. for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only quirks_mode buffer_initial_length depth]
  226. result[iv.intern] = instance_variable_get("@#{iv}")
  227. end
  228. result
  229. end
  230. # Generates a valid JSON document from object +obj+ and returns the
  231. # result. If no valid JSON document can be created this method raises a
  232. # GeneratorError exception.
  233. def generate(obj)
  234. result = obj.to_json(self)
  235. if !@quirks_mode && result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m
  236. raise GeneratorError, "only generation of JSON objects or arrays allowed"
  237. end
  238. result
  239. end
  240. # Return the value returned by method +name+.
  241. def [](name)
  242. __send__ name
  243. end
  244. end
  245. module GeneratorMethods
  246. module Object
  247. # Converts this object to a string (calling #to_s), converts
  248. # it to a JSON string, and returns the result. This is a fallback, if no
  249. # special method #to_json was defined for some object.
  250. def to_json(*) to_s.to_json end
  251. end
  252. module Hash
  253. # Returns a JSON string containing a JSON object, that is unparsed from
  254. # this Hash instance.
  255. # _state_ is a JSON::State object, that can also be used to configure the
  256. # produced JSON string output further.
  257. # _depth_ is used to find out nesting depth, to indent accordingly.
  258. def to_json(state = nil, *)
  259. state = State.from_state(state)
  260. state.check_max_nesting
  261. json_transform(state)
  262. end
  263. private
  264. def json_shift(state)
  265. state.object_nl.empty? or return ''
  266. state.indent * state.depth
  267. end
  268. def json_transform(state)
  269. delim = ','
  270. delim << state.object_nl
  271. result = '{'
  272. result << state.object_nl
  273. depth = state.depth += 1
  274. first = true
  275. indent = !state.object_nl.empty?
  276. each { |key,value|
  277. result << delim unless first
  278. result << state.indent * depth if indent
  279. result << key.to_s.to_json(state)
  280. result << state.space_before
  281. result << ':'
  282. result << state.space
  283. result << value.to_json(state)
  284. first = false
  285. }
  286. depth = state.depth -= 1
  287. result << state.object_nl
  288. result << state.indent * depth if indent if indent
  289. result << '}'
  290. result
  291. end
  292. end
  293. module Array
  294. # Returns a JSON string containing a JSON array, that is unparsed from
  295. # this Array instance.
  296. # _state_ is a JSON::State object, that can also be used to configure the
  297. # produced JSON string output further.
  298. def to_json(state = nil, *)
  299. state = State.from_state(state)
  300. state.check_max_nesting
  301. json_transform(state)
  302. end
  303. private
  304. def json_transform(state)
  305. delim = ','
  306. delim << state.array_nl
  307. result = '['
  308. result << state.array_nl
  309. depth = state.depth += 1
  310. first = true
  311. indent = !state.array_nl.empty?
  312. each { |value|
  313. result << delim unless first
  314. result << state.indent * depth if indent
  315. result << value.to_json(state)
  316. first = false
  317. }
  318. depth = state.depth -= 1
  319. result << state.array_nl
  320. result << state.indent * depth if indent
  321. result << ']'
  322. end
  323. end
  324. module Integer
  325. # Returns a JSON string representation for this Integer number.
  326. def to_json(*) to_s end
  327. end
  328. module Float
  329. # Returns a JSON string representation for this Float number.
  330. def to_json(state = nil, *)
  331. state = State.from_state(state)
  332. case
  333. when infinite?
  334. if state.allow_nan?
  335. to_s
  336. else
  337. raise GeneratorError, "#{self} not allowed in JSON"
  338. end
  339. when nan?
  340. if state.allow_nan?
  341. to_s
  342. else
  343. raise GeneratorError, "#{self} not allowed in JSON"
  344. end
  345. else
  346. to_s
  347. end
  348. end
  349. end
  350. module String
  351. if defined?(::Encoding)
  352. # This string should be encoded with UTF-8 A call to this method
  353. # returns a JSON string encoded with UTF16 big endian characters as
  354. # \u????.
  355. def to_json(state = nil, *args)
  356. state = State.from_state(state)
  357. if encoding == ::Encoding::UTF_8
  358. string = self
  359. else
  360. string = encode(::Encoding::UTF_8)
  361. end
  362. if state.ascii_only?
  363. '"' << JSON.utf8_to_json_ascii(string) << '"'
  364. else
  365. '"' << JSON.utf8_to_json(string) << '"'
  366. end
  367. end
  368. else
  369. # This string should be encoded with UTF-8 A call to this method
  370. # returns a JSON string encoded with UTF16 big endian characters as
  371. # \u????.
  372. def to_json(state = nil, *args)
  373. state = State.from_state(state)
  374. if state.ascii_only?
  375. '"' << JSON.utf8_to_json_ascii(self) << '"'
  376. else
  377. '"' << JSON.utf8_to_json(self) << '"'
  378. end
  379. end
  380. end
  381. # Module that holds the extinding methods if, the String module is
  382. # included.
  383. module Extend
  384. # Raw Strings are JSON Objects (the raw bytes are stored in an
  385. # array for the key "raw"). The Ruby String can be created by this
  386. # module method.
  387. def json_create(o)
  388. o['raw'].pack('C*')
  389. end
  390. end
  391. # Extends _modul_ with the String::Extend module.
  392. def self.included(modul)
  393. modul.extend Extend
  394. end
  395. # This method creates a raw object hash, that can be nested into
  396. # other data structures and will be unparsed as a raw string. This
  397. # method should be used, if you want to convert raw strings to JSON
  398. # instead of UTF-8 strings, e. g. binary data.
  399. def to_json_raw_object
  400. {
  401. JSON.create_id => self.class.name,
  402. 'raw' => self.unpack('C*'),
  403. }
  404. end
  405. # This method creates a JSON text from the result of
  406. # a call to to_json_raw_object of this String.
  407. def to_json_raw(*args)
  408. to_json_raw_object.to_json(*args)
  409. end
  410. end
  411. module TrueClass
  412. # Returns a JSON string for true: 'true'.
  413. def to_json(*) 'true' end
  414. end
  415. module FalseClass
  416. # Returns a JSON string for false: 'false'.
  417. def to_json(*) 'false' end
  418. end
  419. module NilClass
  420. # Returns a JSON string for nil: 'null'.
  421. def to_json(*) 'null' end
  422. end
  423. end
  424. end
  425. end
  426. end