~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to lib/json/pure.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 11:42:29 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124114229-jw2f87rdxlq6gp11
Tags: 1.9.0.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'json/common'
2
 
require 'json/pure/parser'
3
 
require 'json/pure/generator'
4
 
 
5
 
module JSON
6
 
  begin
7
 
    require 'iconv'
8
 
    # An iconv instance to convert from UTF8 to UTF16 Big Endian.
9
 
    UTF16toUTF8 = Iconv.new('utf-8', 'utf-16be') # :nodoc:
10
 
    # An iconv instance to convert from UTF16 Big Endian to UTF8.
11
 
    UTF8toUTF16 = Iconv.new('utf-16be', 'utf-8') # :nodoc:
12
 
    UTF8toUTF16.iconv('no bom')
13
 
  rescue Errno::EINVAL, Iconv::InvalidEncoding
14
 
    # Iconv doesn't support big endian utf-16. Let's try to hack this manually
15
 
    # into the converters.
16
 
    begin
17
 
      old_verbose, $VERBSOSE = $VERBOSE, nil
18
 
      # An iconv instance to convert from UTF8 to UTF16 Big Endian.
19
 
      UTF16toUTF8 = Iconv.new('utf-8', 'utf-16') # :nodoc:
20
 
      # An iconv instance to convert from UTF16 Big Endian to UTF8.
21
 
      UTF8toUTF16 = Iconv.new('utf-16', 'utf-8') # :nodoc:
22
 
      UTF8toUTF16.iconv('no bom')
23
 
      if UTF8toUTF16.iconv("\xe2\x82\xac") == "\xac\x20"
24
 
        swapper = Class.new do
25
 
          def initialize(iconv) # :nodoc:
26
 
            @iconv = iconv
27
 
          end
28
 
 
29
 
          def iconv(string) # :nodoc:
30
 
            result = @iconv.iconv(string)
31
 
            JSON.swap!(result)
32
 
          end
33
 
        end
34
 
        UTF8toUTF16 = swapper.new(UTF8toUTF16) # :nodoc:
35
 
      end
36
 
      if UTF16toUTF8.iconv("\xac\x20") == "\xe2\x82\xac"
37
 
        swapper = Class.new do
38
 
          def initialize(iconv) # :nodoc:
39
 
            @iconv = iconv
40
 
          end
41
 
 
42
 
          def iconv(string) # :nodoc:
43
 
            string = JSON.swap!(string.dup)
44
 
            @iconv.iconv(string)
45
 
          end
46
 
        end
47
 
        UTF16toUTF8 = swapper.new(UTF16toUTF8) # :nodoc:
48
 
      end
49
 
    rescue Errno::EINVAL, Iconv::InvalidEncoding
50
 
      raise MissingUnicodeSupport, "iconv doesn't seem to support UTF-8/UTF-16 conversions"
51
 
    ensure
52
 
      $VERBOSE = old_verbose
53
 
    end
54
 
  rescue LoadError
55
 
    raise MissingUnicodeSupport,
56
 
      "iconv couldn't be loaded, which is required for UTF-8/UTF-16 conversions"
57
 
  end
58
 
 
59
 
  # Swap consecutive bytes of _string_ in place.
60
 
  def self.swap!(string) # :nodoc:
61
 
    0.upto(string.size / 2) do |i|
62
 
      break unless string[2 * i + 1]
63
 
      string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
64
 
    end
65
 
    string
66
 
  end
67
 
 
68
 
  # This module holds all the modules/classes that implement JSON's
69
 
  # functionality in pure ruby.
70
 
  module Pure
71
 
    $DEBUG and warn "Using pure library for JSON."
72
 
    JSON.parser = Parser
73
 
    JSON.generator = Generator
74
 
  end
75
 
end