jsonpp 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +15 -0
  2. data/bin/jsonpp +1 -1
  3. data/okjson.rb +83 -85
  4. metadata +5 -7
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODVjNzlkZDkwMDRiYzEyMzlkNDM3YzNlYzE2NzI0ZDNmZjZhOWE1Yg==
5
+ data.tar.gz: !binary |-
6
+ NmJiOGE3NTM4OGMwN2RkNTdlMWM0N2Y2MWI3Zjg1MGM1OGFmZThlMg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ODE3MjZjMDU4NDVjYmU4ZWQ3ZWVkOWI0MTUyOWU4ZWYyNjk2ZTYwN2ZkOWIx
10
+ NzRhZWZhYzk0ZGE0M2VhYWJlOTY0NThjOWMzYjVjZWZjNjU0YTQwOGVmYTA0
11
+ NjEzMjgxMmM3ZGQzMzU1ZTIxNjlmZWE4NjkwMGU1OTJlODMxM2M=
12
+ data.tar.gz: !binary |-
13
+ OTUwZDcxMTVmMmE3NGMyZjVhNjg2ODA0MmI5ZDFkOTkxNjZjMTdlZGY1Nzdl
14
+ NjQzNWVhMDg0ZWRhM2UyN2UyZjU0YTYzY2VhMzQ0MzUxMTIzZWE1YWE1NDQ1
15
+ ZTcxM2NlNWZlMzg0ZmU3OWFkYTMzZGQ1MTUxYzkyMDUzMjRjMDQ=
data/bin/jsonpp CHANGED
@@ -106,7 +106,7 @@ class JSONPP < PrettyPrint
106
106
 
107
107
  def objenc(obj)
108
108
  bgroup(2, red('{'), red('}'), 1, 1) do
109
- seplist(obj.keys.sort) do |k|
109
+ seplist(obj.keys) do |k|
110
110
  group do
111
111
  pp k
112
112
  text ': '
data/okjson.rb CHANGED
@@ -28,7 +28,7 @@ require 'stringio'
28
28
  # http://golang.org/src/pkg/json/decode.go and
29
29
  # http://golang.org/src/pkg/utf8/utf8.go
30
30
  module OkJson
31
- Upstream = 'LTD7LBKLZWFF7OZK'
31
+ Upstream = '42'
32
32
  extend self
33
33
 
34
34
 
@@ -50,12 +50,49 @@ module OkJson
50
50
  end
51
51
 
52
52
 
53
+ # Encodes x into a json text. It may contain only
54
+ # Array, Hash, String, Numeric, true, false, nil.
55
+ # (Note, this list excludes Symbol.)
56
+ # X itself must be an Array or a Hash.
57
+ # No other value can be encoded, and an error will
58
+ # be raised if x contains any other value, such as
59
+ # Nan, Infinity, Symbol, and Proc, or if a Hash key
60
+ # is not a String.
61
+ # Strings contained in x must be valid UTF-8.
62
+ def encode(x)
63
+ case x
64
+ when Hash then objenc(x)
65
+ when Array then arrenc(x)
66
+ else
67
+ raise Error, 'root value must be an Array or a Hash'
68
+ end
69
+ end
70
+
71
+
72
+ def valenc(x)
73
+ case x
74
+ when Hash then objenc(x)
75
+ when Array then arrenc(x)
76
+ when String then strenc(x)
77
+ when Numeric then numenc(x)
78
+ when true then "true"
79
+ when false then "false"
80
+ when nil then "null"
81
+ else
82
+ raise Error, "cannot encode #{x.class}: #{x.inspect}"
83
+ end
84
+ end
85
+
86
+
87
+ private
88
+
89
+
53
90
  # Parses a "json text" in the sense of RFC 4627.
54
91
  # Returns the parsed value and any trailing tokens.
55
92
  # Note: this is almost the same as valparse,
56
93
  # except that it does not accept atomic values.
57
94
  def textparse(ts)
58
- if ts.length < 0
95
+ if ts.length <= 0
59
96
  raise Error, 'empty'
60
97
  end
61
98
 
@@ -72,7 +109,7 @@ module OkJson
72
109
  # Parses a "value" in the sense of RFC 4627.
73
110
  # Returns the parsed value and any trailing tokens.
74
111
  def valparse(ts)
75
- if ts.length < 0
112
+ if ts.length <= 0
76
113
  raise Error, 'empty'
77
114
  end
78
115
 
@@ -201,21 +238,19 @@ module OkJson
201
238
  # it is the lexeme.
202
239
  def tok(s)
203
240
  case s[0]
204
- when ?{ then ['{', s[0,1], s[0,1]]
205
- when ?} then ['}', s[0,1], s[0,1]]
206
- when ?: then [':', s[0,1], s[0,1]]
207
- when ?, then [',', s[0,1], s[0,1]]
208
- when ?[ then ['[', s[0,1], s[0,1]]
209
- when ?] then [']', s[0,1], s[0,1]]
210
- when ?n then nulltok(s)
211
- when ?t then truetok(s)
212
- when ?f then falsetok(s)
213
- when ?" then strtok(s)
214
- when Spc then [:space, s[0,1], s[0,1]]
215
- when ?\t then [:space, s[0,1], s[0,1]]
216
- when ?\n then [:space, s[0,1], s[0,1]]
217
- when ?\r then [:space, s[0,1], s[0,1]]
218
- else numtok(s)
241
+ when ?{ then ['{', s[0,1], s[0,1]]
242
+ when ?} then ['}', s[0,1], s[0,1]]
243
+ when ?: then [':', s[0,1], s[0,1]]
244
+ when ?, then [',', s[0,1], s[0,1]]
245
+ when ?[ then ['[', s[0,1], s[0,1]]
246
+ when ?] then [']', s[0,1], s[0,1]]
247
+ when ?n then nulltok(s)
248
+ when ?t then truetok(s)
249
+ when ?f then falsetok(s)
250
+ when ?" then strtok(s)
251
+ when Spc, ?\t, ?\n, ?\r then [:space, s[0,1], s[0,1]]
252
+ else
253
+ numtok(s)
219
254
  end
220
255
  end
221
256
 
@@ -228,12 +263,12 @@ module OkJson
228
263
  def numtok(s)
229
264
  m = /-?([1-9][0-9]+|[0-9])([.][0-9]+)?([eE][+-]?[0-9]+)?/.match(s)
230
265
  if m && m.begin(0) == 0
231
- if m[3] && !m[2]
232
- [:val, m[0], Integer(m[1])*(10**Integer(m[3][1..-1]))]
266
+ if !m[2] && !m[3]
267
+ [:val, m[0], Integer(m[0])]
233
268
  elsif m[2]
234
269
  [:val, m[0], Float(m[0])]
235
270
  else
236
- [:val, m[0], Integer(m[0])]
271
+ [:val, m[0], Integer(m[1])*(10**Integer(m[3][1..-1]))]
237
272
  end
238
273
  else
239
274
  []
@@ -265,17 +300,14 @@ module OkJson
265
300
  def unquote(q)
266
301
  q = q[1...-1]
267
302
  a = q.dup # allocate a big enough string
268
- rubydoesenc = false
269
303
  # In ruby >= 1.9, a[w] is a codepoint, not a byte.
270
- if a.class.method_defined?(:force_encoding)
304
+ if rubydoesenc?
271
305
  a.force_encoding('UTF-8')
272
- rubydoesenc = true
273
306
  end
274
307
  r, w = 0, 0
275
308
  while r < q.length
276
309
  c = q[r]
277
- case true
278
- when c == ?\\
310
+ if c == ?\\
279
311
  r += 1
280
312
  if r >= q.length
281
313
  raise Error, "string literal ends with a \"\\\": \"#{q}\""
@@ -308,7 +340,7 @@ module OkJson
308
340
  end
309
341
  end
310
342
  end
311
- if rubydoesenc
343
+ if rubydoesenc?
312
344
  a[w] = '' << uchar
313
345
  w += 1
314
346
  else
@@ -317,7 +349,7 @@ module OkJson
317
349
  else
318
350
  raise Error, "invalid escape char #{q[r]} in \"#{q}\""
319
351
  end
320
- when c == ?", c < Spc
352
+ elsif c == ?" || c < Spc
321
353
  raise Error, "invalid character in string literal \"#{q}\""
322
354
  else
323
355
  # Copy anything else byte-for-byte.
@@ -338,15 +370,14 @@ module OkJson
338
370
  # bytes in string a at position i.
339
371
  # Returns the number of bytes written.
340
372
  def ucharenc(a, i, u)
341
- case true
342
- when u <= Uchar1max
373
+ if u <= Uchar1max
343
374
  a[i] = (u & 0xff).chr
344
375
  1
345
- when u <= Uchar2max
376
+ elsif u <= Uchar2max
346
377
  a[i+0] = (Utag2 | ((u>>6)&0xff)).chr
347
378
  a[i+1] = (Utagx | (u&Umaskx)).chr
348
379
  2
349
- when u <= Uchar3max
380
+ elsif u <= Uchar3max
350
381
  a[i+0] = (Utag3 | ((u>>12)&0xff)).chr
351
382
  a[i+1] = (Utagx | ((u>>6)&Umaskx)).chr
352
383
  a[i+2] = (Utagx | (u&Umaskx)).chr
@@ -383,50 +414,15 @@ module OkJson
383
414
 
384
415
 
385
416
  def nibble(c)
386
- case true
387
- when ?0 <= c && c <= ?9 then c.ord - ?0.ord
388
- when ?a <= c && c <= ?z then c.ord - ?a.ord + 10
389
- when ?A <= c && c <= ?Z then c.ord - ?A.ord + 10
417
+ if ?0 <= c && c <= ?9 then c.ord - ?0.ord
418
+ elsif ?a <= c && c <= ?z then c.ord - ?a.ord + 10
419
+ elsif ?A <= c && c <= ?Z then c.ord - ?A.ord + 10
390
420
  else
391
421
  raise Error, "invalid hex code #{c}"
392
422
  end
393
423
  end
394
424
 
395
425
 
396
- # Encodes x into a json text. It may contain only
397
- # Array, Hash, String, Numeric, true, false, nil.
398
- # (Note, this list excludes Symbol.)
399
- # X itself must be an Array or a Hash.
400
- # No other value can be encoded, and an error will
401
- # be raised if x contains any other value, such as
402
- # Nan, Infinity, Symbol, and Proc, or if a Hash key
403
- # is not a String.
404
- # Strings contained in x must be valid UTF-8.
405
- def encode(x)
406
- case x
407
- when Hash then objenc(x)
408
- when Array then arrenc(x)
409
- else
410
- raise Error, 'root value must be an Array or a Hash'
411
- end
412
- end
413
-
414
-
415
- def valenc(x)
416
- case x
417
- when Hash then objenc(x)
418
- when Array then arrenc(x)
419
- when String then strenc(x)
420
- when Numeric then numenc(x)
421
- when true then "true"
422
- when false then "false"
423
- when nil then "null"
424
- else
425
- raise Error, "cannot encode #{x.class}: #{x.inspect}"
426
- end
427
- end
428
-
429
-
430
426
  def objenc(x)
431
427
  '{' + x.map{|k,v| keyenc(k) + ':' + valenc(v)}.join(',') + '}'
432
428
  end
@@ -451,9 +447,6 @@ module OkJson
451
447
  t.putc(?")
452
448
  r = 0
453
449
 
454
- # In ruby >= 1.9, s[r] is a codepoint, not a byte.
455
- rubydoesenc = s.class.method_defined?(:encoding)
456
-
457
450
  while r < s.length
458
451
  case s[r]
459
452
  when ?" then t.print('\\"')
@@ -465,15 +458,15 @@ module OkJson
465
458
  when ?\t then t.print('\\t')
466
459
  else
467
460
  c = s[r]
468
- case true
469
- when rubydoesenc
461
+ # In ruby >= 1.9, s[r] is a codepoint, not a byte.
462
+ if rubydoesenc?
470
463
  begin
471
464
  c.ord # will raise an error if c is invalid UTF-8
472
465
  t.write(c)
473
466
  rescue
474
467
  t.write(Ustrerr)
475
468
  end
476
- when Spc <= c && c <= ?~
469
+ elsif Spc <= c && c <= ?~
477
470
  t.putc(c)
478
471
  else
479
472
  n = ucharcopy(t, s, r) # ensure valid UTF-8 output
@@ -565,6 +558,11 @@ module OkJson
565
558
  end
566
559
 
567
560
 
561
+ def rubydoesenc?
562
+ ::String.method_defined?(:force_encoding)
563
+ end
564
+
565
+
568
566
  class Utf8Error < ::StandardError
569
567
  end
570
568
 
@@ -573,15 +571,15 @@ module OkJson
573
571
  end
574
572
 
575
573
 
576
- Utagx = 0x80 # 1000 0000
577
- Utag2 = 0xc0 # 1100 0000
578
- Utag3 = 0xe0 # 1110 0000
579
- Utag4 = 0xf0 # 1111 0000
580
- Utag5 = 0xF8 # 1111 1000
581
- Umaskx = 0x3f # 0011 1111
582
- Umask2 = 0x1f # 0001 1111
583
- Umask3 = 0x0f # 0000 1111
584
- Umask4 = 0x07 # 0000 0111
574
+ Utagx = 0b1000_0000
575
+ Utag2 = 0b1100_0000
576
+ Utag3 = 0b1110_0000
577
+ Utag4 = 0b1111_0000
578
+ Utag5 = 0b1111_1000
579
+ Umaskx = 0b0011_1111
580
+ Umask2 = 0b0001_1111
581
+ Umask3 = 0b0000_1111
582
+ Umask4 = 0b0000_0111
585
583
  Uchar1max = (1<<7) - 1
586
584
  Uchar2max = (1<<11) - 1
587
585
  Uchar3max = (1<<16) - 1
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonpp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
5
- prerelease:
4
+ version: 1.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jonas Pfenniger
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-03-23 00:00:00.000000000 Z
11
+ date: 2013-07-16 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: A really small gem that prints your `curl` output nicely
15
14
  email: jonas@pfenniger.name
@@ -23,26 +22,25 @@ files:
23
22
  - okjson.rb
24
23
  homepage: https://github.com/zimbatm/jsonpp
25
24
  licenses: []
25
+ metadata: {}
26
26
  post_install_message:
27
27
  rdoc_options: []
28
28
  require_paths:
29
29
  - lib
30
30
  required_ruby_version: !ruby/object:Gem::Requirement
31
- none: false
32
31
  requirements:
33
32
  - - ! '>='
34
33
  - !ruby/object:Gem::Version
35
34
  version: '0'
36
35
  required_rubygems_version: !ruby/object:Gem::Requirement
37
- none: false
38
36
  requirements:
39
37
  - - ! '>='
40
38
  - !ruby/object:Gem::Version
41
39
  version: '0'
42
40
  requirements: []
43
41
  rubyforge_project:
44
- rubygems_version: 1.8.23
42
+ rubygems_version: 2.0.3
45
43
  signing_key:
46
- specification_version: 3
44
+ specification_version: 4
47
45
  summary: Command-line JSON pretty-printer
48
46
  test_files: []