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

Unified Diff: Source/bindings/scripts/IRToFromJSON.pm

Issue 16296004: JSON export/import in generate-bindings.pl (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: IDL: Perl to/from JSON export/import 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 side-by-side diff with in-line comments
Download patch
Index: Source/bindings/scripts/IRToFromJSON.pm
diff --git a/Source/bindings/scripts/IRToFromJSON.pm b/Source/bindings/scripts/IRToFromJSON.pm
new file mode 100644
index 0000000000000000000000000000000000000000..e0d81b2f3df8b581bb111f9d205a57ce5dac467a
--- /dev/null
+++ b/Source/bindings/scripts/IRToFromJSON.pm
@@ -0,0 +1,118 @@
+# Copyright (C) 2013 Google Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+# * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+# Library to convert the Perl Intermediate Representation (IR) of IDLs
+# to and from JSON, for:
+# 1. Modularity between frontend parser and backend code generator;
+# 2. Porting to Python, so can connect Perl scripts and Python scripts.
+
+package IRToFromJSON;
haraken 2013/06/12 03:20:21 I'd call this JSONSerializer.pm. IR sounds unclear
Nils Barth (inactive) 2013/06/12 03:57:43 Done.
+
+use strict;
+use warnings;
+
+require Exporter;
+use vars qw(@ISA @EXPORT_OK);
+@ISA = qw(Exporter);
+@EXPORT_OK = qw(IRToJSON JSONToIR);
haraken 2013/06/12 03:20:21 I'd remove the code for controlling exports. You c
Nils Barth (inactive) 2013/06/12 03:57:43 Done.
+
+use Class::Struct;
+use JSON -convert_blessed_universally; # IR contains blessed references
+use Data::Dumper;
haraken 2013/06/12 03:20:21 Remove this.
Nils Barth (inactive) 2013/06/12 03:57:43 Oops (>.<) Done.
+
+use IDLParser;
haraken 2013/06/12 03:20:21 Remove this.
Nils Barth (inactive) 2013/06/12 03:57:43 Ok. Needed so we have classes for object construct
+
+
+sub IRToJSON
haraken 2013/06/12 03:20:21 SerializeJSON might be a better name.
Nils Barth (inactive) 2013/06/12 03:57:43 Done.
+{
+ my $document = shift;
+ my $json = JSON->new->utf8;
+ # Blessed references for objects, canonical to order (so can compare)
dominicc (has gone to gerrit) 2013/06/11 22:56:30 What does this mean? "Blessed references for obje
Nils Barth (inactive) 2013/06/12 02:56:12 Clarified.
+ $json = $json->allow_blessed->convert_blessed->canonical();
+ return $json->encode($document);
+}
+
+sub rawIRToCleanIR
dominicc (has gone to gerrit) 2013/06/11 22:56:30 How about a brief comment (one line) explaining wh
Nils Barth (inactive) 2013/06/12 02:56:12 I've renamed "rawIR" and "cleanIR" to "jsonIR" and
+{
+ my $rawIR = shift;
+ if (ref $rawIR eq "ARRAY") {
+ my $cleanIR = [];
+ foreach my $element (@$rawIR) {
+ push(@$cleanIR, rawIRToCleanIR($element));
+ }
+ return $cleanIR;
+ }
+ if (ref $rawIR eq "HASH") {
+ if (!scalar %{$rawIR}) {
+ return {};
+ }
+ # overloadedIndex is not part of domFunction struct,
+ # (just a hash key, not an object member), so special case
+ my $overload = -1;
+ my $classAndKeyFirst = (keys %{$rawIR})[0];
dominicc (has gone to gerrit) 2013/06/11 22:56:30 I think this code can be made much simpler. This
Nils Barth (inactive) 2013/06/12 02:56:12 Rewritten, with helper function. It's still a bit
+ if ($classAndKeyFirst eq "overloadedIndex") {
dominicc (has gone to gerrit) 2013/06/11 22:56:30 Why special-case overloadedIndex *only* if it is t
Nils Barth (inactive) 2013/06/12 02:56:12 We're just looking for the class name here, so we
+ $overload = $rawIR->{$classAndKeyFirst};
+ $classAndKeyFirst = (keys %{$rawIR})[1];
+ }
+ (my $class, my $firstKey) = (split('::', $classAndKeyFirst));
+ my $keysValuesRef = {};
+ if ($class ne $classAndKeyFirst) { # object
dominicc (has gone to gerrit) 2013/06/11 22:56:30 I'm having trouble understanding this. It looks li
Nils Barth (inactive) 2013/06/12 02:56:12 This tests if the splitting achieved anything (i.e
+ foreach my $classAndKey (keys %{$rawIR}) {
+ if ($classAndKey eq "overloadedIndex") {
+ $overload = $rawIR->{$classAndKey};
+ } else {
+ (my $newClass, my $key) = split('::', $classAndKey);
+ $keysValuesRef->{$key} = rawIRToCleanIR($rawIR->{$classAndKey});
+ }
+ }
+ my $object = $class->new(%$keysValuesRef); # Build object
+ if ($overload != -1) {
+ $object->{overloadedIndex} = $overload;
+ }
+ return $object;
+ } else { # just hash
+ foreach my $key (keys %{$rawIR}) {
+ $keysValuesRef->{$key} = rawIRToCleanIR($rawIR->{$key});
+ }
+ return $keysValuesRef;
+ }
+ }
+ die "Unexpected reference type: " . ref $rawIR . "\n" if ref $rawIR;
+ return $rawIR;
+}
+
+sub JSONToIR
haraken 2013/06/12 03:20:21 deserializeJSON might be a better name.
+{
+ my $jsonText = shift;
+ my $json = JSON->new->utf8;
+ my $rawIR = $json->decode($jsonText);
+ return rawIRToCleanIR($rawIR);
+}
+
+1;

Powered by Google App Engine
This is Rietveld 408576698