summaryrefslogtreecommitdiff
path: root/ext/psych/lib/psych/visitors/json_tree.rb
blob: 0ec1678a3943ee2d38c8303cf23d5cd22c9a026b (plain)
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
module Psych
  module Visitors
    class JSONTree < YAMLTree
      def initialize options = {}, emitter = Psych::JSON::TreeBuilder.new
        super
      end

      def visit_NilClass o
        @emitter.scalar 'null', nil, nil, true, false, Nodes::Scalar::PLAIN
      end

      def visit_Integer o
        @emitter.scalar o.to_s, nil, nil, true, false, Nodes::Scalar::PLAIN
      end

      def visit_Float o
        return super if o.nan? || o.infinite?
        visit_Integer o
      end

      def visit_String o
        @emitter.scalar o.to_s, nil, nil, false, true, Nodes::Scalar::ANY
      end
      alias :visit_Symbol :visit_String

      private
    end
  end
end