FAQ

svn commit: r883945 - in /lucene/lucy/trunk: core/Lucy/Test/Util/TestJson.bp core/Lucy/Test/Util/TestJson.c core/Lucy/Util/Json.bp perl/Build.PL perl/lib/Lucy.pm perl/lib/Lucy/Test.pm perl/t/core/035-json.t perl/xs/Lucy/Util/ perl/xs/Lucy/Util/Json.c

Marvin
Nov 25, 2009 at 1:07 am
Author: marvin
Date: Wed Nov 25 01:06:50 2009
New Revision: 883945

URL: http://svn.apache.org/viewvc?rev=883945&view=rev
Log:
Commit LUCY-74, adding the Lucy::Util::Json API and the Perl binding
implementation for it.

Added:
lucene/lucy/trunk/core/Lucy/Test/Util/TestJson.bp (with props)
lucene/lucy/trunk/core/Lucy/Test/Util/TestJson.c (with props)
lucene/lucy/trunk/core/Lucy/Util/Json.bp (with props)
lucene/lucy/trunk/perl/t/core/035-json.t (with props)
lucene/lucy/trunk/perl/xs/Lucy/Util/
lucene/lucy/trunk/perl/xs/Lucy/Util/Json.c (with props)
Modified:
lucene/lucy/trunk/perl/Build.PL
lucene/lucy/trunk/perl/lib/Lucy.pm
lucene/lucy/trunk/perl/lib/Lucy/Test.pm

Added: lucene/lucy/trunk/core/Lucy/Test/Util/TestJson.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Util/TestJson.bp?rev=883945&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Util/TestJson.bp (added)
+++ lucene/lucy/trunk/core/Lucy/Test/Util/TestJson.bp Wed Nov 25 01:06:50 2009
@@ -0,0 +1,22 @@
+parcel Lucy;
+
+inert class Lucy::Test::Util::TestJson {
+ inert void
+ run_tests();
+}
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Test/Util/TestJson.bp
------------------------------------------------------------------------------
svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Test/Util/TestJson.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Util/TestJson.c?rev=883945&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Util/TestJson.c (added)
+++ lucene/lucy/trunk/core/Lucy/Test/Util/TestJson.c Wed Nov 25 01:06:50 2009
@@ -0,0 +1,113 @@
+#define C_LUCY_CHARBUF
+#include "Lucy/Util/ToolSet.h"
+
+#include "Lucy/Test.h"
+#include "Lucy/Test/Util/TestJson.h"
+#include "Lucy/Util/Json.h"
+#include "Lucy/Store/FileHandle.h"
+#include "Lucy/Store/RAMFolder.h"
+
+static CharBuf foo = ZCB_LITERAL("foo");
+static CharBuf bar = ZCB_LITERAL("bar");
+static CharBuf boffo = ZCB_LITERAL("boffo");
+
+/* Create a test data structure including at least one each of Hash, VArray,
+ * and CharBuf. */
+static Obj*
+S_make_dump()
+{
+ Hash *dump = Hash_new(0);
+ Hash_Store_Str(dump, "foo", 3, (Obj*)CB_newf("foo"));
+ Hash_Store_Str(dump, "stuff", 5, (Obj*)VA_new(0));
+ return (Obj*)dump;
+}
+
+static void
+test_to_and_from(TestBatch *batch)
+{
+ Obj *dump = S_make_dump();
+ CharBuf *json = Json_to_json(dump);
+ Obj *got = Json_from_json(json);
+ ASSERT_TRUE(batch, got != NULL && Obj_Equals(dump, got),
+ "Round trip through to_json and from_json");
+ DECREF(dump);
+ DECREF(json);
+ DECREF(got);
+}
+
+static void
+test_spew_and_slurp(TestBatch *batch)
+{
+ Obj *dump = S_make_dump();
+ Folder *folder = (Folder*)RAMFolder_new(NULL);
+ bool_t result;
+ Obj *got;
+
+ result = Json_spew_json(dump, folder, &foo);
+ ASSERT_TRUE(batch, result, "spew_json returns true on success");
+ ASSERT_TRUE(batch, Folder_Exists(folder, (CharBuf*)&foo),
+ "spew_json wrote file");
+
+ got = Json_slurp_json(folder, &foo);
+ ASSERT_TRUE(batch, got && Obj_Equals(dump, got),
+ "Round trip through spew_json and slurp_json");
+ DECREF(got);
+
+ Err_set_error(NULL);
+ result = Json_spew_json(dump, folder, &foo);
+ ASSERT_FALSE(batch, result, "Can't spew_json when file exists");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "Failed spew_json sets Err_error");
+
+ Err_set_error(NULL);
+ got = Json_slurp_json(folder, &bar);
+ ASSERT_TRUE(batch, got == NULL,
+ "slurp_json returns NULL when file doesn't exist");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "Failed slurp_json sets Err_error");
+
+ {
+ FileHandle *fh = Folder_Open_FileHandle(folder, &boffo,
+ FH_CREATE | FH_WRITE_ONLY );
+ FH_Write(fh, "garbage", 7);
+ DECREF(fh);
+ }
+ Err_set_error(NULL);
+ got = Json_slurp_json(folder, &boffo);
+ ASSERT_TRUE(batch, got == NULL,
+ "slurp_json returns NULL when file doesn't contain valid JSON");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "Failed slurp_json sets Err_error");
+ DECREF(got);
+
+ DECREF(dump);
+ DECREF(folder);
+}
+
+void
+TestJson_run_tests()
+{
+ TestBatch *batch = Test_new_batch("TestJson", 10, NULL);
+
+ PLAN(batch);
+ test_to_and_from(batch);
+ test_spew_and_slurp(batch);
+
+ batch->destroy(batch);
+}
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Test/Util/TestJson.c
------------------------------------------------------------------------------
svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Util/Json.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Util/Json.bp?rev=883945&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Util/Json.bp (added)
+++ lucene/lucy/trunk/core/Lucy/Util/Json.bp Wed Nov 25 01:06:50 2009
@@ -0,0 +1,50 @@
+parcel Lucy;
+
+/** Encode/decode JSON.
+ *
+ * Provides utility functions for encoding/decoding JSON.
+ */
+class Lucy::Util::Json extends Lucy::Object::Obj {
+
+ /** Encode <code>dump</code> as JSON.
+ */
+ inert incremented CharBuf*
+ to_json(Obj *dump);
+
+ /** Decode the supplied JSON and return a data structure made
+ * of Hashes, VArrays, and CharBufs.
+ */
+ inert incremented Obj*
+ from_json(CharBuf *json);
+
+ /** Encode <code>dump</code> as JSON and attempt to write to the indicated
+ * file.
+ * @return true if the write succeeds, false on failure (sets Err_error).
+ */
+ inert bool_t
+ spew_json(Obj *dump, Folder *folder, const CharBuf *path);
+
+ /** Decode the JSON in the file at <code>path</code> and return a data
+ * structure made of Hashes, VArrays, and CharBufs. Returns NULL and sets
+ * Err_error if the file can't be can't be opened or if the file doesn't
+ * contain valid JSON.
+ */
+ inert incremented Obj*
+ slurp_json(Folder *folder, const CharBuf *path);
+}
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Util/Json.bp
------------------------------------------------------------------------------
svn:eol-style = native

Modified: lucene/lucy/trunk/perl/Build.PL
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/Build.PL?rev=883945&r1=883944&r2=883945&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/Build.PL (original)
+++ lucene/lucy/trunk/perl/Build.PL Wed Nov 25 01:06:50 2009
@@ -12,6 +12,7 @@
dist_version_from => 'lib/Lucy.pm',
dist_abstract_from => 'lib/Lucy.pm',
create_makefile_pl => 'passthrough',
+ requires => { 'JSON::XS' => 1.53, },
build_requires => {
'ExtUtils::CBuilder' => 0.18,
'ExtUtils::ParseXS' => 2.16,

Modified: lucene/lucy/trunk/perl/lib/Lucy.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/lib/Lucy.pm?rev=883945&r1=883944&r2=883945&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/lib/Lucy.pm (original)
+++ lucene/lucy/trunk/perl/lib/Lucy.pm Wed Nov 25 01:06:50 2009
@@ -30,6 +30,8 @@
blessed
nfreeze
thaw
+ to_lucy
+ to_perl
);
}
}
@@ -114,6 +116,57 @@
}
}

+{
+ package Lucy::Util::Json;
+ use Lucy::Util::ToolSet qw( to_lucy );
+
+ use JSON::XS qw();
+
+ my $json_encoder = JSON::XS->new->pretty(1)->canonical(1);
+
+ sub slurp_json {
+ my ( undef, %args ) = @_;
+ my $instream = $args{folder}->open_in( $args{path} )
+ or return;
+ my $len = $instream->length;
+ my $json;
+ $instream->read( $json, $len );
+ my $result = eval { to_lucy( $json_encoder->decode($json) ) };
+ if ( $@ or !$result ) {
+ Lucy::Object::Err->set_error(
+ Lucy::Object::Err->new( $@ || "Failed to decode JSON" )
+ );
+ return;
+ }
+ return $result;
+ }
+
+ sub spew_json {
+ my ( undef, %args ) = @_;
+ my $json = eval { $json_encoder->encode( $args{'dump'} ) };
+ if ( !defined $json ) {
+ Lucy::Object::Err->set_error(
+ Lucy::Object::Err->new($@) );
+ return 0;
+ }
+ my $outstream = $args{folder}->open_out( $args{path} );
+ return 0 unless $outstream;
+ $outstream->print($json);
+ eval { $outstream->close; };
+ return 0 if $@;
+ return 1;
+ }
+
+ sub to_json {
+ my ( undef, $dump ) = @_;
+ return $json_encoder->encode($dump);
+ }
+
+ sub from_json {
+ return to_lucy( $json_encoder->decode( $_[1] ) );
+ }
+}
+
1;

__END__
@@ -134,6 +187,42 @@
RETVAL
END_XS_CODE

+my $toolset_xs_code = <<'END_XS_CODE';
+MODULE = Lucy PACKAGE = Lucy::Util::ToolSet
+
+SV*
+to_lucy(sv)
+ SV *sv;
+CODE:
+{
+ lucy_Obj *obj = XSBind_perl_to_lucy(sv);
+ RETVAL = LUCY_OBJ_TO_SV_NOINC(obj);
+}
+OUTPUT: RETVAL
+
+SV*
+to_perl(sv)
+ SV *sv;
+CODE:
+{
+ if (sv_isobject(sv) && sv_derived_from(sv, "Lucy::Object::Obj")) {
+ IV tmp = SvIV(SvRV(sv));
+ lucy_Obj* obj = INT2PTR(lucy_Obj*, tmp);
+ RETVAL = XSBind_lucy_to_perl(obj);
+ }
+ else {
+ RETVAL = newSVsv(sv);
+ }
+}
+OUTPUT: RETVAL
+END_XS_CODE
+
+Boilerplater::Binding::Perl::Class->register(
+ parcel => "Lucy",
+ class_name => "Lucy::Util::Toolset",
+ xs_code => $toolset_xs_code,
+);
+
Boilerplater::Binding::Perl::Class->register(
parcel => "Lucy",
class_name => "Lucy",

Modified: lucene/lucy/trunk/perl/lib/Lucy/Test.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/lib/Lucy/Test.pm?rev=883945&r1=883944&r2=883945&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/lib/Lucy/Test.pm (original)
+++ lucene/lucy/trunk/perl/lib/Lucy/Test.pm Wed Nov 25 01:06:50 2009
@@ -62,6 +62,9 @@
else if (strEQ(package, "TestIndexFileNames")) {
lucy_TestIxFileNames_run_tests();
}
+ else if (strEQ(package, "TestJson")) {
+ lucy_TestJson_run_tests();
+ }
else if (strEQ(package, "TestNumberUtils")) {
lucy_TestNumUtil_run_tests();
}

Added: lucene/lucy/trunk/perl/t/core/035-json.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/t/core/035-json.t?rev=883945&view=auto
==============================================================================
--- lucene/lucy/trunk/perl/t/core/035-json.t (added)
+++ lucene/lucy/trunk/perl/t/core/035-json.t Wed Nov 25 01:06:50 2009
@@ -0,0 +1,6 @@
+use strict;
+use warnings;
+
+use Lucy::Test;
+Lucy::Test::run_tests("TestJson");
+

Propchange: lucene/lucy/trunk/perl/t/core/035-json.t
------------------------------------------------------------------------------
svn:eol-style = native

Added: lucene/lucy/trunk/perl/xs/Lucy/Util/Json.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/xs/Lucy/Util/Json.c?rev=883945&view=auto
==============================================================================
--- lucene/lucy/trunk/perl/xs/Lucy/Util/Json.c (added)
+++ lucene/lucy/trunk/perl/xs/Lucy/Util/Json.c Wed Nov 25 01:06:50 2009
@@ -0,0 +1,54 @@
+#include "Lucy/Util/ToolSet.h"
+
+#include "Lucy/Util/Json.h"
+#include "Lucy/Object/Host.h"
+#include "Lucy/Store/Folder.h"
+
+bool_t
+Json_spew_json(Obj *dump, Folder *folder, const CharBuf *path)
+{
+ bool_t result = (bool_t)Host_callback_i64(JSON, "spew_json", 3,
+ ARG_OBJ("dump", dump), ARG_OBJ("folder", folder),
+ ARG_STR("path", path));
+ if (!result) { ERR_ADD_FRAME(Err_get_error()); }
+ return result;
+}
+
+Obj*
+Json_slurp_json(Folder *folder, const CharBuf *path)
+{
+ Obj *dump = Host_callback_obj(JSON, "slurp_json", 2,
+ ARG_OBJ("folder", folder), ARG_STR("path", path));
+ if (!dump) { ERR_ADD_FRAME(Err_get_error()); }
+ return dump;
+}
+
+CharBuf*
+Json_to_json(Obj *dump)
+{
+ return Host_callback_str(JSON, "to_json", 1,
+ ARG_OBJ("dump", dump));
+}
+
+Obj*
+Json_from_json(CharBuf *json)
+{
+ return Host_callback_obj(JSON, "from_json", 1,
+ ARG_STR("json", json));
+}
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Propchange: lucene/lucy/trunk/perl/xs/Lucy/Util/Json.c
------------------------------------------------------------------------------
svn:eol-style = native
reply

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post

1 user in discussion

Marvin: 1 post