Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: Source/bindings/scripts/JSONSerializer.pm

Issue 16296004: JSON export/import in generate-bindings.pl (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Revised again Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 # Copyright (C) 2013 Google Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
6 #
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
12 # distribution.
13 # * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
30 # Library to convert the Perl Intermediate Representation (IR) of IDLs
dominicc (has gone to gerrit) 2013/06/12 04:39:33 "Perl Intermediate Representation (IR)" could be b
Nils Barth (inactive) 2013/06/12 09:15:27 Rewritten, and renamed to remove redundant "IR" th
31 # to and from JSON, for:
32 # 1. Modularity between frontend parser and backend code generator;
33 # 2. Porting to Python, so can connect Perl scripts and Python scripts.
34
35 package JSONSerializer;
36
37 use strict;
38 use warnings;
39
40 require Exporter;
41 use vars qw(@ISA @EXPORT);
42 @ISA = qw(Exporter);
43 @EXPORT = qw(serializeJSON deserializeJSON);
haraken 2013/06/12 04:46:31 Do you need these exports?
Nils Barth (inactive) 2013/06/12 09:15:27 If it's a package, yes. I've removed "package" and
44
45 use Class::Struct;
46 use JSON -convert_blessed_universally; # IR contains objects (blessed reference s)
47
48
49 sub serializeJSON
50 {
51 my $document = shift;
52 my $json = JSON->new->utf8;
53 # JSON.pm defaults to barfing on objects (blessed references) and returning keys in indeterminate order; set options to change this:
dominicc (has gone to gerrit) 2013/06/12 04:39:33 Be consistent--you don't wrap here but do on line
Nils Barth (inactive) 2013/06/12 09:15:27 Done. (Consistently wrap multi-line.)
54 # allow_blessed: don't barf when encounter blessed (but default to return nu ll)
55 # convert_blessed: convert blessed reference as if unblessed (rather than re turning null)
56 # canonical: sort keys when writing JSON, so JSON always in same order,
57 # so can compare output between runs or between Perl and Python
haraken 2013/06/12 04:46:31 Regarding comments here and there, please use more
Nils Barth (inactive) 2013/06/12 09:15:27 (Reworded to change "barf" to "die", usual Perl te
58 $json = $json->allow_blessed->convert_blessed->canonical();
59 return $json->encode($document);
60 }
61
62 sub deserializeJSON
63 {
64 my $jsonText = shift;
65 my $json = JSON->new->utf8;
66 my $jsonIR = $json->decode($jsonText);
67 return jsonIRToPerlIR($jsonIR);
dominicc (has gone to gerrit) 2013/06/12 04:39:33 Maybe $json should be $jsonDecoder or something? D
Nils Barth (inactive) 2013/06/12 09:15:27 I'm following usage in docs, which calls this obje
68 }
69
70 sub jsonIRToPerlIR
71 {
72 my $jsonIR = shift;
73 if (ref $jsonIR eq "ARRAY") {
74 my $perlIR = [];
75 foreach my $element (@$jsonIR) {
76 push(@$perlIR, jsonIRToPerlIR($element));
77 }
78 return $perlIR;
79 }
80 if (ref $jsonIR eq "HASH") {
81 if (!scalar %{$jsonIR}) {
haraken 2013/06/12 04:46:31 my @keys = keys %$jsonIR; return {} if @keys == 0;
Nils Barth (inactive) 2013/06/12 09:15:27 (>.<) Good point (changed here and below).
82 return {};
83 }
84 # Detect objects as hashes where all keys are of form CLASS::KEY
85 # One exception: overloadedIndex is not part of domFunction struct,
86 # (just a hash key, not an object member), so need to special case
87 my $firstKey = (keys %{$jsonIR})[0];
haraken 2013/06/12 04:46:31 $keys[0]
Nils Barth (inactive) 2013/06/12 09:15:27 Done.
88 $firstKey = (keys %{$jsonIR})[1] if $firstKey eq "overloadedIndex";
dominicc (has gone to gerrit) 2013/06/12 04:39:33 Suggestion: Make a method to get the class name fo
haraken 2013/06/12 04:46:31 $keys[1]
Nils Barth (inactive) 2013/06/12 09:15:27 Done.
89 my $isObject = $firstKey =~ /::/;
dominicc (has gone to gerrit) 2013/06/12 04:39:33 Nice use of explaining variable.
Nils Barth (inactive) 2013/06/12 09:15:27 Thanks!
90
91 if ($isObject) {
92 my $class = (split('::', $firstKey))[0];
93 return jsonHashToPerlObject($jsonIR, $class);
94 } else { # just hash
95 my $hashRef = {};
96 foreach my $key (keys %{$jsonIR}) {
haraken 2013/06/12 04:46:31 foreach my $key (@keys)
Nils Barth (inactive) 2013/06/12 09:15:27 Done.
97 $hashRef->{$key} = jsonIRToPerlIR($jsonIR->{$key});
98 }
99 return $hashRef;
100 }
101 }
102 die "Unexpected reference type: " . ref $jsonIR . "\n" if ref $jsonIR;
103 return $jsonIR;
104 }
105
106 sub jsonHashToPerlObject
107 {
108 # JSON.pm serializes hash objects of class CLASS as a hash with keys CLASS:: KEY1, CLASS::KEY2, etc.
109 # When deserializing, need to rebuild objects by stripping prefix and callin g the constructor
110 my $jsonHash = shift;
111 my $class = shift;
112
113 my %keysValues = ();
114 foreach my $classAndKey (keys %{$jsonHash}) {
115 next if $classAndKey eq "overloadedIndex";
116 my $key = (split('::', $classAndKey))[1];
117 $keysValues{$key} = jsonIRToPerlIR($jsonHash->{$classAndKey});
118 }
119 my $object = $class->new(%keysValues); # Build object
120 # overloadedIndex not part of constructor; add separately
121 $object->{overloadedIndex} = $jsonHash->{overloadedIndex} if exists $jsonHas h->{overloadedIndex};
122 return $object;
123 }
124
125 1;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698