Merge branch '1.2'
authorPetri Lehtinen <petri@digip.org>
Tue, 23 Mar 2010 06:15:19 +0000 (08:15 +0200)
committerPetri Lehtinen <petri@digip.org>
Tue, 23 Mar 2010 06:15:19 +0000 (08:15 +0200)
26 files changed:
.gitignore
CHANGES
LICENSE
configure.ac
doc/apiref.rst
doc/conf.py
src/Makefile.am
src/dump.c
src/hashtable.c
src/hashtable.h
src/jansson.h
src/jansson.hpp [new file with mode: 0644]
src/jansson.ipp [new file with mode: 0644]
src/jansson_private.h
src/value.c
test/.gitignore
test/bin/json_process.c
test/suites/api/Makefile.am
test/suites/api/check-exports
test/suites/api/run
test/suites/api/test_cpp.cpp [new file with mode: 0644]
test/suites/api/test_cpp.json [new file with mode: 0644]
test/suites/api/test_object.c
test/suites/encoding-flags/preserve-order/env [new file with mode: 0644]
test/suites/encoding-flags/preserve-order/input [new file with mode: 0644]
test/suites/encoding-flags/preserve-order/output [new file with mode: 0644]

index 4c4115c..c33c1fc 100644 (file)
@@ -1,3 +1,4 @@
+*~
 *.o
 *.a
 .libs
diff --git a/CHANGES b/CHANGES
index 02f28c4..dcbf6a0 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,12 @@
+Version 1.3 (in development)
+============================
+
+* New encoding flags:
+
+  - ``JSON_PRESERVE_ORDER``: Preserve the insertion order of object
+    keys.
+
+
 Version 1.2
 ===========
 
diff --git a/LICENSE b/LICENSE
index 552b349..b5c2887 100644 (file)
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,5 @@
 Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
+Copyright (c) 2010 Sean Middleditch <sean@middleditch.us>
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
index 6bc9064..6d21268 100644 (file)
@@ -1,5 +1,5 @@
 AC_PREREQ([2.59])
-AC_INIT([jansson], [1.2], [petri@digip.org])
+AC_INIT([jansson], [1.2+], [petri@digip.org])
 
 AM_INIT_AUTOMAKE([1.10 foreign])
 
@@ -8,6 +8,7 @@ AC_CONFIG_HEADERS([config.h])
 
 # Checks for programs.
 AC_PROG_CC
+AC_PROG_CXX
 AC_PROG_LIBTOOL
 
 # Checks for libraries.
index a2a0794..ebe00ea 100644 (file)
@@ -493,6 +493,16 @@ The following functions implement an iteration protocol for objects:
    Returns an opaque iterator which can be used to iterate over all
    key-value pairs in *object*, or *NULL* if *object* is empty.
 
+.. cfunction:: void *json_object_iter_at(json_t *object, const char *key)
+
+   Like :cfunc:`json_object_iter()`, but returns an iterator to the
+   key-value pair in *object* whose key is equal to *key*, or NULL if
+   *key* is not found in *object*. Iterating forward to the end of
+   *object* only yields all key-value pairs of the object if *key*
+   happens to be the first key in the underlying hash table.
+
+   .. versionadded:: 1.3
+
 .. cfunction:: void *json_object_iter_next(json_t *object, void *iter)
 
    Returns an iterator pointing to the next key-value pair in *object*
@@ -509,6 +519,21 @@ The following functions implement an iteration protocol for objects:
 
    Extract the associated value from *iter*.
 
+.. cfunction:: int json_object_iter_set(json_t *object, void *iter, json_t *value)
+
+   Set the value of the key-value pair in *object*, that is pointed to
+   by *iter*, to *value*.
+
+   .. versionadded:: 1.3
+
+.. cfunction:: int json_object_iter_set_new(json_t *object, void *iter, json_t *value)
+
+   Like :cfunc:`json_object_iter_set()`, but steals the reference to
+   *value*. This is useful when *value* is newly created and not used
+   after the call.
+
+   .. versionadded:: 1.3
+
 The iteration protocol can be used for example as follows::
 
    /* obj is a JSON object */
@@ -570,6 +595,14 @@ can be ORed together to obtain *flags*.
 
    .. versionadded:: 1.2
 
+``JSON_PRESERVE_ORDER``
+   If this flag is used, object keys in the output are sorted into the
+   same order in which they were first inserted to the object. For
+   example, decoding a JSON text and then encoding with this flag
+   preserves the order of object keys.
+
+   .. versionadded:: 1.3
+
 The following functions perform the actual JSON encoding. The result
 is in UTF-8.
 
index f4ec126..2a95aad 100644 (file)
@@ -52,7 +52,7 @@ copyright = u'2009, 2010 Petri Lehtinen'
 # The short X.Y version.
 version = '1.2'
 # The full version, including alpha/beta/rc tags.
-release = '1.2'
+release = '1.2+'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.
index e70077b..92fc90c 100644 (file)
@@ -1,4 +1,4 @@
-include_HEADERS = jansson.h
+include_HEADERS = jansson.h jansson.hpp jansson.ipp
 
 lib_LTLIBRARIES = libjansson.la
 libjansson_la_SOURCES = \
index bc06dfd..a862cba 100644 (file)
@@ -153,9 +153,16 @@ static int dump_string(const char *str, int ascii, dump_func dump, void *data)
     return dump("\"", 1, data);
 }
 
-static int object_key_cmp(const void *key1, const void *key2)
+static int object_key_compare_keys(const void *key1, const void *key2)
 {
-    return strcmp(*(const char **)key1, *(const char **)key2);
+    return strcmp((*(const object_key_t **)key1)->key,
+                  (*(const object_key_t **)key2)->key);
+}
+
+static int object_key_compare_serials(const void *key1, const void *key2)
+{
+    return (*(const object_key_t **)key1)->serial -
+           (*(const object_key_t **)key2)->serial;
 }
 
 static int do_dump(const json_t *json, unsigned long flags, int depth,
@@ -289,36 +296,40 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,
             if(dump_indent(flags, depth + 1, 0, dump, data))
                 return -1;
 
-            if(flags & JSON_SORT_KEYS)
+            if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER)
             {
-                /* Sort keys */
-
-                const char **keys;
+                const object_key_t **keys;
                 unsigned int size;
                 unsigned int i;
+                int (*cmp_func)(const void *, const void *);
 
                 size = json_object_size(json);
-                keys = malloc(size * sizeof(const char *));
+                keys = malloc(size * sizeof(object_key_t *));
                 if(!keys)
                     return -1;
 
                 i = 0;
                 while(iter)
                 {
-                    keys[i] = json_object_iter_key(iter);
+                    keys[i] = jsonp_object_iter_fullkey(iter);
                     iter = json_object_iter_next((json_t *)json, iter);
                     i++;
                 }
                 assert(i == size);
 
-                qsort(keys, size, sizeof(const char *), object_key_cmp);
+                if(flags & JSON_SORT_KEYS)
+                    cmp_func = object_key_compare_keys;
+                else
+                    cmp_func = object_key_compare_serials;
+
+                qsort(keys, size, sizeof(object_key_t *), cmp_func);
 
                 for(i = 0; i < size; i++)
                 {
                     const char *key;
                     json_t *value;
 
-                    key = keys[i];
+                    key = keys[i]->key;
                     value = json_object_get(json, key);
                     assert(value);
 
index 1f8abf4..4b58b26 100644 (file)
@@ -247,31 +247,39 @@ int hashtable_set(hashtable_t *hashtable, void *key, void *value)
     bucket_t *bucket;
     unsigned int hash, index;
 
-    hash = hashtable->hash_key(key);
-
-    /* if the key already exists, delete it */
-    hashtable_do_del(hashtable, key, hash);
-
     /* rehash if the load ratio exceeds 1 */
     if(hashtable->size >= num_buckets(hashtable))
         if(hashtable_do_rehash(hashtable))
             return -1;
 
-    pair = malloc(sizeof(pair_t));
-    if(!pair)
-        return -1;
-
-    pair->key = key;
-    pair->value = value;
-    pair->hash = hash;
-    list_init(&pair->list);
-
+    hash = hashtable->hash_key(key);
     index = hash % num_buckets(hashtable);
     bucket = &hashtable->buckets[index];
+    pair = hashtable_find_pair(hashtable, bucket, key, hash);
+
+    if(pair)
+    {
+        if(hashtable->free_key)
+            hashtable->free_key(key);
+        if(hashtable->free_value)
+            hashtable->free_value(pair->value);
+        pair->value = value;
+    }
+    else
+    {
+        pair = malloc(sizeof(pair_t));
+        if(!pair)
+            return -1;
+
+        pair->key = key;
+        pair->value = value;
+        pair->hash = hash;
+        list_init(&pair->list);
 
-    insert_to_bucket(hashtable, bucket, &pair->list);
+        insert_to_bucket(hashtable, bucket, &pair->list);
 
-    hashtable->size++;
+        hashtable->size++;
+    }
     return 0;
 }
 
@@ -318,6 +326,22 @@ void *hashtable_iter(hashtable_t *hashtable)
     return hashtable_iter_next(hashtable, &hashtable->list);
 }
 
+void *hashtable_iter_at(hashtable_t *hashtable, const void *key)
+{
+    pair_t *pair;
+    unsigned int hash;
+    bucket_t *bucket;
+
+    hash = hashtable->hash_key(key);
+    bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
+
+    pair = hashtable_find_pair(hashtable, bucket, key, hash);
+    if(!pair)
+        return NULL;
+
+    return &pair->list;
+}
+
 void *hashtable_iter_next(hashtable_t *hashtable, void *iter)
 {
     list_t *list = (list_t *)iter;
@@ -337,3 +361,13 @@ void *hashtable_iter_value(void *iter)
     pair_t *pair = list_to_pair((list_t *)iter);
     return pair->value;
 }
+
+void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value)
+{
+    pair_t *pair = list_to_pair((list_t *)iter);
+
+    if(hashtable->free_value)
+        hashtable->free_value(pair->value);
+
+    pair->value = value;
+}
index e055d9f..f03a769 100644 (file)
@@ -161,6 +161,17 @@ void hashtable_clear(hashtable_t *hashtable);
 void *hashtable_iter(hashtable_t *hashtable);
 
 /**
+ * hashtable_iter_at - Return an iterator at a specific key
+ *
+ * @hashtable: The hashtable object
+ * @key: The key that the iterator should point to
+ *
+ * Like hashtable_iter() but returns an iterator pointing to a
+ * specific key.
+ */
+void *hashtable_iter_at(hashtable_t *hashtable, const void *key);
+
+/**
  * hashtable_iter_next - Advance an iterator
  *
  * @hashtable: The hashtable object
@@ -185,4 +196,12 @@ void *hashtable_iter_key(void *iter);
  */
 void *hashtable_iter_value(void *iter);
 
+/**
+ * hashtable_iter_set - Set the value pointed by an iterator
+ *
+ * @iter: The iterator
+ * @value: The value to set
+ */
+void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value);
+
 #endif
index 78094a7..24b4949 100644 (file)
@@ -83,9 +83,11 @@ int json_object_del(json_t *object, const char *key);
 int json_object_clear(json_t *object);
 int json_object_update(json_t *object, json_t *other);
 void *json_object_iter(json_t *object);
+void *json_object_iter_at(json_t *object, const char *key);
 void *json_object_iter_next(json_t *object, void *iter);
 const char *json_object_iter_key(void *iter);
 json_t *json_object_iter_value(void *iter);
+int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
 
 static inline
 int json_object_set(json_t *object, const char *key, json_t *value)
@@ -99,6 +101,12 @@ int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
     return json_object_set_new_nocheck(object, key, json_incref(value));
 }
 
+static inline
+int json_object_iter_set(json_t *object, void *iter, json_t *value)
+{
+    return json_object_iter_set_new(object, iter, json_incref(value));
+}
+
 unsigned int json_array_size(const json_t *array);
 json_t *json_array_get(const json_t *array, unsigned int index);
 int json_array_set_new(json_t *array, unsigned int index, json_t *value);
@@ -165,6 +173,7 @@ json_t *json_load_file(const char *path, json_error_t *error);
 #define JSON_COMPACT        0x100
 #define JSON_ENSURE_ASCII   0x200
 #define JSON_SORT_KEYS      0x400
+#define JSON_PRESERVE_ORDER 0x800
 
 char *json_dumps(const json_t *json, unsigned long flags);
 int json_dumpf(const json_t *json, FILE *output, unsigned long flags);
diff --git a/src/jansson.hpp b/src/jansson.hpp
new file mode 100644 (file)
index 0000000..c8db40d
--- /dev/null
@@ -0,0 +1,317 @@
+// Copyright (c) 2010 Sean Middleditch <sean@middleditch.us>
+// Copyright (c) 2010 Petri Lehtinen <petri@digip.org>
+//
+// Jansson is free software; you can redistribute it and/or modify
+// it under the terms of the MIT license. See LICENSE for details.
+
+#ifndef JANSSON_HPP
+#define JANSSON_HPP
+
+#include <string>
+#include <ostream>
+#include <istream>
+#include <sstream>
+
+// Included so that standard functions don't end up in namespace json
+#include <cstdio>
+
+// For free()
+#include <cstdlib>
+
+namespace json {
+    // include Jansson C library into the json namespace
+#   include <jansson.h>
+
+    class Iterator;
+    class Value;
+
+    // implementation details; do not use directly
+    namespace detail {
+        class ElementProxy;
+        class PropertyProxy;
+
+        // base class for JSON value interface
+        template <typename _Base>
+        class ValueBase : public _Base {
+        public:
+            // empty constructor
+            ValueBase() : _Base() {}
+
+            // copy constructor
+            ValueBase(const _Base& base) : _Base(base) {}
+
+            // create reference to value
+            ValueBase(json_t* json) : _Base(json) {}
+
+            // assignment operator
+            inline ValueBase& operator=(const Value& value);
+
+            // check value type
+            inline bool is_undefined() const;
+            inline bool is_object() const;
+            inline bool is_array() const;
+            inline bool is_string() const;
+            inline bool is_integer() const;
+            inline bool is_real() const;
+            inline bool is_number() const;
+            inline bool is_true() const;
+            inline bool is_false() const;
+            inline bool is_boolean() const;
+            inline bool is_null() const;
+
+            // get size of array or object
+            inline unsigned int size() const;
+
+            // get value at array index (const version)
+            inline const Value at(unsigned int index) const;
+
+            inline const Value operator[](signed int index) const;
+            inline const Value operator[](unsigned int index) const;
+            inline const Value operator[](signed short index) const;
+            inline const Value operator[](unsigned short index) const;
+            inline const Value operator[](signed long index) const;
+            inline const Value operator[](unsigned long index) const;
+
+            // get value at array index (non-const version)
+            inline ValueBase<ElementProxy> at(unsigned int index);
+
+            inline ValueBase<ElementProxy> operator[](signed int index);
+            inline ValueBase<ElementProxy> operator[](unsigned int index);
+            inline ValueBase<ElementProxy> operator[](signed short index);
+            inline ValueBase<ElementProxy> operator[](unsigned short index);
+            inline ValueBase<ElementProxy> operator[](signed long index);
+            inline ValueBase<ElementProxy> operator[](unsigned long index);
+
+            // get object property (const version)
+            inline const Value get(const char* key) const;
+
+            inline const Value get(const std::string& key) const;
+            inline const Value operator[](const char* key) const;
+            inline const Value operator[](const std::string& key) const;
+
+            // get object property (non-const version)
+            inline ValueBase<PropertyProxy> get(const char* key);
+
+            inline ValueBase<PropertyProxy> get(const std::string& key);
+            inline ValueBase<PropertyProxy> operator[](const char* key);
+            inline ValueBase<PropertyProxy> operator[](const std::string& key);
+
+            // clear all array/object values
+            inline void clear();
+
+            // get value cast to specified type
+            inline const char* as_cstring() const;
+            inline std::string as_string() const;
+            inline int as_integer() const;
+            inline double as_real() const;
+            inline double as_number() const;
+            inline bool as_boolean() const;
+
+            // set an object property (converts value to object is not one already)
+            inline _Base& set_key(const char* key, const Value& value);
+
+            inline _Base& set_key(const std::string& key, const Value& value);
+
+            // set an array index (converts value to object is not one already)
+            inline _Base& set_at(unsigned int index, const Value& value);
+
+            // delete an object key
+            inline _Base& del_key(const char* key);
+
+            inline _Base& del_key(const std::string& key);
+
+            // delete an item from an array by index
+            inline _Base& del_at(unsigned int index);
+
+            // insert an item into an array at a given index
+            inline _Base& insert_at(unsigned int index, const Value& value);
+
+            // write the value to a file
+            inline int dump_file(const char* path, int flags = 0) const;
+            inline int dump_file(const std::string& path, int flags = 0) const;
+
+            // write the value to a string (caller must deallocate with free()!)
+            inline char* dumps(int flags = 0) const;
+        };
+
+        // represents any JSON value, private base
+        class Basic {
+        public:
+            // construct new Value with an undefined value
+            Basic() : _value(0) {}
+
+            // copy constructor
+            Basic(const Basic& value) : _value(json_incref(value._value)) {}
+
+            // make a reference to an existing json_t value
+            explicit Basic(json_t* value) : _value(json_incref(value)) {}
+
+            // free Value resources
+            inline ~Basic();
+
+            // copy an existing Value
+            inline Basic& operator=(const Basic& e);
+
+            // get the underlying json_t
+            inline json_t* as_json() const;
+
+            // take ownership of a json_t (does not increase reference count)
+            inline static Basic take_ownership(json_t* json);
+
+        protected:
+            // internal value pointer
+            json_t* _value;
+        };
+
+        // proxies an array element
+        class ElementProxy {
+        public:
+            inline ElementProxy(json_t* array, unsigned int index);
+            inline ElementProxy(const ElementProxy& other);
+            inline ~ElementProxy();
+
+            // assign to the proxied element
+            inline ElementProxy& operator=(const Value& value);
+
+            // get the proxied element
+            inline json_t* as_json() const;
+
+        private:
+            // array object we wrap
+            json_t* _array;
+
+            // index of property
+            unsigned int _index;
+        };
+
+        // proxies an object property
+        class PropertyProxy {
+        public:
+            inline PropertyProxy(json_t* object, const char *key);
+            inline PropertyProxy(const PropertyProxy& other);
+            inline ~PropertyProxy();
+
+            // assign to the proxied element
+            inline PropertyProxy& operator=(const Value& value);
+
+            // get the proxied element
+            inline json_t* as_json() const;
+
+        private:
+            // array object we wrap
+            json_t* _object;
+
+            // iterator pointing to property
+            void* _iter;
+
+            // key of property
+            char* _key;
+        };
+
+    } // namespace json::detail
+
+    // represents any JSON value
+    class Value : public detail::ValueBase<detail::Basic> {
+    public:
+        // construct Value from input
+        explicit inline Value(const char* value);
+        explicit inline Value(const std::string& value);
+        explicit inline Value(bool value);
+        explicit inline Value(signed int value);
+        explicit inline Value(unsigned int value);
+        explicit inline Value(signed short value);
+        explicit inline Value(unsigned short value);
+        explicit inline Value(signed long value);
+        explicit inline Value(unsigned long value);
+        explicit inline Value(float value);
+        explicit inline Value(double value);
+
+        // empty constructor
+        Value() : detail::ValueBase<detail::Basic>() {}
+
+        // copy constructor for base
+        Value(const detail::Basic& value) : detail::ValueBase<detail::Basic>(value) {}
+
+        // copy constructor for base
+        Value(const detail::ValueBase<detail::Basic>& value) : detail::ValueBase<detail::Basic>(value) {}
+
+        // copy constructor
+        Value(const Value& value) : detail::ValueBase<detail::Basic>(value) {}
+
+        // create reference to value
+        explicit Value(json_t* json) : detail::ValueBase<detail::Basic>(json) {}
+    };
+
+    // iterators over a JSON object
+    class Iterator {
+    public:
+        // construct a new iterator for a given object
+        inline Iterator(const Value& value);
+
+        // construct a new iterator for a given object
+        inline Iterator(const detail::ValueBase<detail::PropertyProxy>& value);
+
+        // increment iterator
+        inline void next();
+
+        inline Iterator& operator++();
+
+        // test if iterator is still valid
+        inline bool valid() const;
+
+        inline operator bool() const;
+
+        // get key
+        inline const char* ckey() const;
+
+        inline std::string key() const;
+
+        // get value
+        inline const Value value() const;
+
+        // dereference value
+        inline const Value operator*() const;
+
+    private:
+        // disallow copying
+        Iterator(const Iterator&);
+        Iterator& operator=(const Iterator&);
+
+        // object being iterated over
+        Value _object;
+
+        // iterator value
+        void* _iter;
+    };
+
+    // create a new empty object
+    inline Value object();
+
+    // create a new empty array
+    inline Value array();
+
+    // create a new null value
+    inline Value null();
+
+    // load a file as a JSON value
+    inline Value load_file(const char* path, json_error_t* error = 0);
+    inline Value load_file(const std::string& path, json_error_t* error = 0);
+
+    // load a string as a JSON value
+    inline Value loads(const char* string, json_error_t* error = 0);
+    inline Value loads(const std::string& string, json_error_t* error = 0);
+
+} // namespace json
+
+// stream JSON value out -- inefficient and not recommended for production use
+inline std::ostream& operator<<(std::ostream& os, const json::Value& value);
+
+// read JSON value -- inefficient and not recommended for production use
+inline std::istream& operator>>(std::istream& is, json::Value& value);
+
+// include implementation code
+#define IN_JANSSON_HPP
+#include "jansson.ipp"
+#undef IN_JANSSON_HPP
+
+#endif // defined(JANSSON_HPP)
diff --git a/src/jansson.ipp b/src/jansson.ipp
new file mode 100644 (file)
index 0000000..e965ef7
--- /dev/null
@@ -0,0 +1,525 @@
+// Copyright (c) 2010 Sean Middleditch <sean@middleditch.us>
+// Copyright (c) 2010 Petri Lehtinen <petri@digip.org>
+//
+// Jansson is free software; you can redistribute it and/or modify
+// it under the terms of the MIT license. See LICENSE for details.
+
+#ifndef IN_JANSSON_HPP
+#error "jansson.ipp may only be included from jansson.hpp"
+#endif
+
+#include <string.h>
+
+namespace json {
+    namespace detail {
+        // assignment operator
+        template <typename _Base>
+        ValueBase<_Base>& ValueBase<_Base>::operator=(const Value& value) {
+            _Base::operator=(value);
+            return *this;
+        }
+
+        // check value type
+        template <typename _Base>
+        bool ValueBase<_Base>::is_undefined() const {
+            return _Base::as_json() == 0;
+        }
+
+        template <typename _Base>
+        bool ValueBase<_Base>::is_object() const {
+            return json_is_object(_Base::as_json());
+        }
+
+        template <typename _Base>
+        bool ValueBase<_Base>::is_array() const {
+            return json_is_array(_Base::as_json());
+        }
+
+        template <typename _Base>
+        bool ValueBase<_Base>::is_string() const {
+            return json_is_string(_Base::as_json());
+        }
+
+        template <typename _Base>
+        bool ValueBase<_Base>::is_integer() const {
+            return json_is_integer(_Base::as_json());
+        }
+
+        template <typename _Base>
+        bool ValueBase<_Base>::is_real() const {
+            return json_is_real(_Base::as_json());
+        }
+
+        template <typename _Base>
+        bool ValueBase<_Base>::is_number() const {
+            return json_is_number(_Base::as_json());
+        }
+
+        template <typename _Base>
+        bool ValueBase<_Base>::is_true() const {
+            return json_is_true(_Base::as_json());
+        }
+
+        template <typename _Base>
+        bool ValueBase<_Base>::is_false() const {
+            return json_is_false(_Base::as_json());
+        }
+
+        template <typename _Base>
+        bool ValueBase<_Base>::is_boolean() const {
+            return json_is_boolean(_Base::as_json());
+        }
+
+        template <typename _Base>
+        bool ValueBase<_Base>::is_null() const {
+            return json_is_null(_Base::as_json());
+        }
+
+        // get size of array or object
+        template <typename _Base>
+        unsigned int ValueBase<_Base>::size() const {
+            if (is_object())
+                return json_object_size(_Base::as_json());
+            else
+                return json_array_size(_Base::as_json());
+        }
+
+        // get value at array index (const version)
+        template <typename _Base>
+        const Value ValueBase<_Base>::at(unsigned int index) const {
+            return Value(json_array_get(_Base::as_json(), index));
+        }
+
+        template <typename _Base>
+        const Value ValueBase<_Base>::operator[](signed int index) const { return at(index); }
+        template <typename _Base>
+        const Value ValueBase<_Base>::operator[](unsigned int index) const { return at(index); }
+        template <typename _Base>
+        const Value ValueBase<_Base>::operator[](signed short index) const { return at(index); }
+        template <typename _Base>
+        const Value ValueBase<_Base>::operator[](unsigned short index) const { return at(index); }
+        template <typename _Base>
+        const Value ValueBase<_Base>::operator[](signed long index) const { return at(index); }
+        template <typename _Base>
+        const Value ValueBase<_Base>::operator[](unsigned long index) const { return at(index); }
+
+        // get value at array index (non-const version)
+        template <typename _Base>
+        ValueBase<ElementProxy> ValueBase<_Base>::at(unsigned int index) {
+            return ElementProxy(_Base::as_json(), index);
+        }
+
+        template <typename _Base>
+        ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed int index) {
+            return at(index);
+        }
+
+        template <typename _Base>
+        ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned int index) {
+            return at(index);
+        }
+
+        template <typename _Base>
+        ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed short index) {
+            return at(index);
+        }
+
+        template <typename _Base>
+        ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned short index) {
+            return at(index);
+        }
+
+        template <typename _Base>
+        ValueBase<ElementProxy> ValueBase<_Base>::operator[](signed long index) {
+            return at(index);
+        }
+
+        template <typename _Base>
+        ValueBase<ElementProxy> ValueBase<_Base>::operator[](unsigned long index) {
+            return at(index);
+        }
+
+        // get object property (const version)
+        template <typename _Base>
+        const Value ValueBase<_Base>::get(const char* key) const {
+            return Value(json_object_get(_Base::as_json(), key));
+        }
+
+        template <typename _Base>
+        const Value ValueBase<_Base>::get(const std::string& key) const {
+            return get(key.c_str());
+        }
+
+        template <typename _Base>
+        const Value ValueBase<_Base>::operator[](const char* key) const {
+            return get(key);
+        }
+
+        template <typename _Base>
+        const Value ValueBase<_Base>::operator[](const std::string& key) const {
+            return get(key.c_str());
+        }
+
+        // get object property (non-const version)
+        template <typename _Base>
+        ValueBase<PropertyProxy> ValueBase<_Base>::get(const char* key) {
+            return PropertyProxy(_Base::as_json(), key);
+        }
+
+        template <typename _Base>
+        ValueBase<PropertyProxy> ValueBase<_Base>::get(const std::string& key) {
+            return get(key.c_str());
+        }
+
+        template <typename _Base>
+        ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const char* key) {
+            return get(key);
+        }
+
+        template <typename _Base>
+        ValueBase<PropertyProxy> ValueBase<_Base>::operator[](const std::string& key) {
+            return get(key.c_str());
+        }
+
+        // clear all array/object values
+        template <typename _Base>
+        void ValueBase<_Base>::clear() {
+            if (is_object())
+                json_object_clear(_Base::as_json());
+            else
+                json_array_clear(_Base::as_json());
+        }
+
+        // get value cast to specified type
+        template <typename _Base>
+        const char* ValueBase<_Base>::as_cstring() const {
+            return json_string_value(_Base::as_json());
+        }
+
+        template <typename _Base>
+        std::string ValueBase<_Base>::as_string() const {
+            const char* tmp = as_cstring();
+            return tmp == 0 ? "" : tmp;
+        }
+
+        template <typename _Base>
+        int ValueBase<_Base>::as_integer() const {
+            return json_integer_value(_Base::as_json());
+        }
+
+        template <typename _Base>
+        double ValueBase<_Base>::as_real() const {
+            return json_real_value(_Base::as_json());
+        }
+
+        template <typename _Base>
+        double ValueBase<_Base>::as_number() const {
+            return json_number_value(_Base::as_json());
+        }
+
+        template <typename _Base>
+        bool ValueBase<_Base>::as_boolean() const {
+            return is_true();
+        }
+
+        // set an object property (converts value to object is not one already)
+        template <typename _Base>
+        _Base& ValueBase<_Base>::set_key(const char* key, const Value& value) {
+            json_object_set(_Base::as_json(), key, value._Base::as_json());
+            return *this;
+        }
+
+        template <typename _Base>
+        _Base& ValueBase<_Base>::set_key(const std::string& key, const Value& value) {
+            return set_key(key.c_str(), value);
+        }
+
+        // set an array index (converts value to object is not one already)
+        template <typename _Base>
+        _Base& ValueBase<_Base>::set_at(unsigned int index, const Value& value) {
+            if (index == size())
+                json_array_append(_Base::as_json(), value._Base::as_json());
+            else
+                json_array_set(_Base::as_json(), index, value._Base::as_json());
+            return *this;
+        }
+
+        // delete an object key
+        template <typename _Base>
+        _Base& ValueBase<_Base>::del_key(const char* key) {
+            json_object_del(_Base::as_json(), key);
+            return *this;
+        }
+
+        template <typename _Base>
+        _Base& ValueBase<_Base>::del_key(const std::string& key) {
+            return del_key(key.c_str());
+        }
+
+        // delete an item from an array by index
+        template <typename _Base>
+        _Base& ValueBase<_Base>::del_at(unsigned int index) {
+            json_array_remove(_Base::as_json(), index);
+            return *this;
+        }
+
+        // insert an item into an array at a given index
+        template <typename _Base>
+        _Base& ValueBase<_Base>::insert_at(unsigned int index, const Value& value) {
+            json_array_insert(_Base::as_json(), index, value._Base::as_json());
+            return *this;
+        }
+
+        // write the value to a file
+        template <typename _Base>
+        int ValueBase<_Base>::dump_file(const char* path, int flags) const {
+            return json_dump_file(_Base::as_json(), path, flags);
+        }
+
+        template <typename _Base>
+        int ValueBase<_Base>::dump_file(const std::string& path, int flags) const {
+            return dump_file(path.c_str(), flags);
+        }
+
+        // write the value to a string (caller must deallocate with free()!)
+        template <typename _Base>
+        char* ValueBase<_Base>::dumps(int flags) const {
+            return json_dumps(_Base::as_json(), flags);
+        }
+
+        Basic::~Basic() {
+            json_decref(_value);
+        }
+
+        // copy an existing Value
+        Basic& Basic::operator=(const Basic& e) {
+            if (&e != this) {
+                json_decref(_value);
+                _value = json_incref(e._value);
+            }
+            return *this;
+        }
+
+        // get the underlying json_t
+        json_t* Basic::as_json() const {
+            return _value;
+        }
+
+        // take ownership of a json_t (does not increase reference count)
+        Basic Basic::take_ownership(json_t* json) {
+            Basic v;
+            v._value = json;
+            return v;
+        }
+
+        ElementProxy::ElementProxy(json_t* array, unsigned int index)
+            : _array(array), _index(index) {
+            json_incref(_array);
+        }
+
+        ElementProxy::ElementProxy(const ElementProxy& other) 
+            : _array(other._array), _index(other._index) {
+            json_incref(_array);
+        }
+
+        ElementProxy::~ElementProxy() {
+            json_decref(_array);
+        }
+
+        // assign value to proxied array element
+        ElementProxy& ElementProxy::operator=(const Value& value) {
+            json_array_set(_array, _index, value.as_json());
+            return *this;
+        }
+
+        // get the proxied element
+        json_t* ElementProxy::as_json() const {
+            return json_array_get(_array, _index);
+        }
+
+        PropertyProxy::PropertyProxy(json_t* object, const char* key)
+            : _object(object), _key(0) {
+            _iter = json_object_iter_at(object, key);
+            if(!_iter)
+                _key = strdup(key);
+            json_incref(_object);
+        }
+
+        PropertyProxy::PropertyProxy(const PropertyProxy& other)
+            : _object(other._object), _iter(other._iter), _key(0) {
+            if(other._key)
+                _key = strdup(other._key);
+            json_incref(_object);
+        }
+
+        PropertyProxy::~PropertyProxy() {
+            free(_key);
+            json_decref(_object);
+        }
+
+        // assign value to proxied object property
+        PropertyProxy& PropertyProxy::operator=(const Value& value) {
+            if(_iter)
+                json_object_iter_set(_object, _iter, value.as_json());
+            else
+                json_object_set(_object, _key, value.as_json());
+            return *this;
+        }
+
+        json_t* PropertyProxy::as_json() const {
+            if(_iter)
+                return json_object_iter_value(_iter);
+            else
+                return json_object_get(_object, _key);
+        }
+
+    } // namespace json::detail
+
+    // construct Value::Value input
+    Value::Value(const char* value) {
+        _value = json_string(value);
+    }
+
+    Value::Value(const std::string& value) {
+        _value = json_string(value.c_str());
+    }
+
+    Value::Value(bool value) {
+        _value = value ? json_true() : json_false();
+    }
+
+    Value::Value(signed int value) {
+        _value = json_integer(value);
+    }
+
+    Value::Value(unsigned int value) {
+        _value = json_integer(value);
+    }
+
+    Value::Value(signed short value) {
+        _value = json_integer(value);
+    }
+
+    Value::Value(unsigned short value) {
+        _value = json_integer(value);
+    }
+
+    Value::Value(signed long value) {
+        _value = json_integer(value);
+    }
+
+    Value::Value(unsigned long value) {
+        _value = json_integer(value);
+    }
+
+    Value::Value(float value) {
+        _value = json_real(value);
+    }
+
+    Value::Value(double value) {
+        _value = json_real(value);
+    }
+
+    // construct a new iterator for a given object
+    Iterator::Iterator(const Value& value) : _object(value), _iter(0) {
+        _iter = json_object_iter(_object.as_json());
+    }
+
+    // construct a new iterator for a given object
+    Iterator::Iterator(const detail::ValueBase<detail::PropertyProxy>& value) :
+        _object(value.as_json()), _iter(0) {
+        _iter = json_object_iter(_object.as_json());
+    }
+
+    // increment iterator
+    void Iterator::next() {
+        _iter = json_object_iter_next(_object.as_json(), _iter);
+    }
+
+    Iterator& Iterator::operator++() { next(); return *this; }
+
+    // test if iterator is still valid
+    bool Iterator::valid() const {
+        return _iter != 0;
+    }
+
+    Iterator::operator bool() const {
+        return valid();
+    }
+
+    // get key
+    const char* Iterator::ckey() const {
+        return json_object_iter_key(_iter);
+    }
+
+    std::string Iterator::key() const {
+        return ckey();
+    }
+
+    // get value
+    const Value Iterator::value() const {
+        return Value(json_object_iter_value(_iter));
+    }
+
+    // dereference value
+    const Value Iterator::operator*() const {
+        return value();
+    }
+
+    // create a new empty object
+    Value object() {
+        return Value::take_ownership(json_object());
+    }
+
+    // create a new empty array
+    Value array() {
+        return Value::take_ownership(json_array());
+    }
+
+    // create a new null value
+    Value null() {
+        return Value::take_ownership(json_null());
+    }
+
+    // load a file as a JSON value
+    Value load_file(const char* path, json_error_t* error) {
+        return Value::take_ownership(json_load_file(path, error));
+    }
+
+    Value load_file(const std::string& path, json_error_t* error) {
+        return load_file(path.c_str(), error);
+    }
+
+    // load a string as a JSON value
+    Value loads(const char* string, json_error_t* error) {
+        return Value::take_ownership(json_loads(string, error));
+    }
+
+    Value loads(const std::string& string, json_error_t* error) {
+        return loads(string.c_str(), error);
+    }
+
+} // namespace json
+
+// stream JSON value out
+std::ostream& operator<<(std::ostream& os, const json::Value& value) {
+    // get the temporary serialize string
+    char* tmp = value.dumps();
+    if (tmp != 0) {
+        // stream temp string out and release it
+        os << tmp;
+        free(tmp);
+    }
+    return os;
+}
+
+// read JSON value
+std::istream& operator>>(std::istream& is, json::Value& value) {
+    // buffer the remaining bytes into a single string for Jansson
+    std::stringstream tmp;
+    while (is)
+        tmp << static_cast<char>(is.get());
+    // parse the buffered string
+    value = json::loads(tmp.str().c_str());
+    return is;
+}
index 3045956..4490702 100644 (file)
@@ -17,6 +17,7 @@
 typedef struct {
     json_t json;
     hashtable_t hashtable;
+    unsigned long serial;
     int visited;
 } json_object_t;
 
@@ -49,4 +50,11 @@ typedef struct {
 #define json_to_real(json_)   container_of(json_, json_real_t, json)
 #define json_to_integer(json_) container_of(json_, json_integer_t, json)
 
+typedef struct {
+    unsigned long serial;
+    char key[];
+} object_key_t;
+
+const object_key_t *jsonp_object_iter_fullkey(void *iter);
+
 #endif
index 3fa7ee3..7c41c89 100644 (file)
@@ -25,9 +25,16 @@ static inline void json_init(json_t *json, json_type type)
 
 /*** object ***/
 
-static unsigned int hash_string(const void *key)
+/* This macro just returns a pointer that's a few bytes backwards from
+   string. This makes it possible to pass a pointer to object_key_t
+   when only the string inside it is used, without actually creating
+   an object_key_t instance. */
+#define string_to_key(string)  container_of(string, object_key_t, key)
+
+static unsigned int hash_key(const void *ptr)
 {
-    const char *str = (const char *)key;
+    const char *str = ((const object_key_t *)ptr)->key;
+
     unsigned int hash = 5381;
     unsigned int c;
 
@@ -40,9 +47,10 @@ static unsigned int hash_string(const void *key)
     return hash;
 }
 
-static int string_equal(const void *key1, const void *key2)
+static int key_equal(const void *ptr1, const void *ptr2)
 {
-    return strcmp((const char *)key1, (const char *)key2) == 0;
+    return strcmp(((const object_key_t *)ptr1)->key,
+                  ((const object_key_t *)ptr2)->key) == 0;
 }
 
 static void value_decref(void *value)
@@ -57,13 +65,14 @@ json_t *json_object(void)
         return NULL;
     json_init(&object->json, JSON_OBJECT);
 
-    if(hashtable_init(&object->hashtable, hash_string, string_equal,
+    if(hashtable_init(&object->hashtable, hash_key, key_equal,
                       free, value_decref))
     {
         free(object);
         return NULL;
     }
 
+    object->serial = 0;
     object->visited = 0;
 
     return &object->json;
@@ -94,12 +103,13 @@ json_t *json_object_get(const json_t *json, const char *key)
         return NULL;
 
     object = json_to_object(json);
-    return hashtable_get(&object->hashtable, key);
+    return hashtable_get(&object->hashtable, string_to_key(key));
 }
 
 int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
 {
     json_object_t *object;
+    object_key_t *k;
 
     if(!key || !value)
         return -1;
@@ -111,7 +121,14 @@ int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
     }
     object = json_to_object(json);
 
-    if(hashtable_set(&object->hashtable, strdup(key), value))
+    k = malloc(sizeof(object_key_t) + strlen(key) + 1);
+    if(!k)
+        return -1;
+
+    k->serial = object->serial++;
+    strcpy(k->key, key);
+
+    if(hashtable_set(&object->hashtable, k, value))
     {
         json_decref(value);
         return -1;
@@ -139,7 +156,7 @@ int json_object_del(json_t *json, const char *key)
         return -1;
 
     object = json_to_object(json);
-    return hashtable_del(&object->hashtable, key);
+    return hashtable_del(&object->hashtable, string_to_key(key));
 }
 
 int json_object_clear(json_t *json)
@@ -190,6 +207,17 @@ void *json_object_iter(json_t *json)
     return hashtable_iter(&object->hashtable);
 }
 
+void *json_object_iter_at(json_t *json, const char *key)
+{
+    json_object_t *object;
+
+    if(!key || !json_is_object(json))
+        return NULL;
+
+    object = json_to_object(json);
+    return hashtable_iter_at(&object->hashtable, string_to_key(key));
+}
+
 void *json_object_iter_next(json_t *json, void *iter)
 {
     json_object_t *object;
@@ -201,12 +229,20 @@ void *json_object_iter_next(json_t *json, void *iter)
     return hashtable_iter_next(&object->hashtable, iter);
 }
 
+const object_key_t *jsonp_object_iter_fullkey(void *iter)
+{
+    if(!iter)
+        return NULL;
+
+    return hashtable_iter_key(iter);
+}
+
 const char *json_object_iter_key(void *iter)
 {
     if(!iter)
         return NULL;
 
-    return (const char *)hashtable_iter_key(iter);
+    return jsonp_object_iter_fullkey(iter)->key;
 }
 
 json_t *json_object_iter_value(void *iter)
@@ -217,6 +253,19 @@ json_t *json_object_iter_value(void *iter)
     return (json_t *)hashtable_iter_value(iter);
 }
 
+int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
+{
+    json_object_t *object;
+
+    if(!json_is_object(json) || !iter || !value)
+        return -1;
+
+    object = json_to_object(json);
+    hashtable_iter_set(&object->hashtable, iter, value);
+
+    return 0;
+}
+
 static int json_object_equal(json_t *object1, json_t *object2)
 {
     void *iter;
index a960c50..065b4ea 100644 (file)
@@ -7,3 +7,4 @@ suites/api/test_load
 suites/api/test_number
 suites/api/test_object
 suites/api/test_simple
+suites/api/test_cpp
index 809c4d4..77407e5 100644 (file)
@@ -53,6 +53,9 @@ int main(int argc, char *argv[])
     if(getenv_int("JSON_ENSURE_ASCII"))
         flags |= JSON_ENSURE_ASCII;
 
+    if(getenv_int("JSON_PRESERVE_ORDER"))
+        flags |= JSON_PRESERVE_ORDER;
+
     if(getenv_int("JSON_SORT_KEYS"))
         flags |= JSON_SORT_KEYS;
 
index 64523c0..ddeb147 100644 (file)
@@ -7,7 +7,8 @@ check_PROGRAMS = \
        test_load \
        test_simple \
        test_number \
-       test_object
+       test_object \
+       test_cpp
 
 test_array_SOURCES = test_array.c util.h
 test_copy_SOURCES = test_copy.c util.h
@@ -15,6 +16,7 @@ test_load_SOURCES = test_load.c util.h
 test_simple_SOURCES = test_simple.c util.h
 test_number_SOURCES = test_number.c util.h
 test_object_SOURCES = test_object.c util.h
+test_cpp_SOURCES = test_cpp.cpp
 
 AM_CPPFLAGS = -I$(top_srcdir)/src
 AM_CFLAGS = -Wall -Werror
index 178abeb..a6d877f 100755 (executable)
@@ -39,9 +39,11 @@ json_object_del
 json_object_clear
 json_object_update
 json_object_iter
+json_object_iter_at
 json_object_iter_next
 json_object_iter_key
 json_object_iter_value
+json_object_iter_set_new
 json_dumps
 json_dumpf
 json_dump_file
index e01148d..ff53f35 100755 (executable)
@@ -6,16 +6,29 @@
 # it under the terms of the MIT license. See LICENSE for details.
 
 is_test() {
-    [ "${test_name%.c}" != "$test_name" ] && return 0
-    [ -x $test_path -a ! -f $test_path.c ] && return 0
-    return 1
+    case "$test_name" in
+        *.c|*.cpp|check-exports)
+            return 0
+            ;;
+        *)
+            return 1
+            ;;
+    esac
 }
 
 run_test() {
-    if [ -x $test_path ]; then
+    if [ "$test_name" = "check-exports" ]; then
         test_log=$test_log $test_path >$test_log/stdout 2>$test_log/stderr
     else
-        $test_runner $suite_builddir/${test_name%.c} \
+        case "$test_name" in
+            *.c)
+                test_bin=${test_name%.c}
+                ;;
+            *.cpp)
+                test_bin=${test_name%.cpp}
+                ;;
+        esac
+        $test_runner $suite_builddir/$test_bin \
             >$test_log/stdout \
             2>$test_log/stderr \
             || return 1
diff --git a/test/suites/api/test_cpp.cpp b/test/suites/api/test_cpp.cpp
new file mode 100644 (file)
index 0000000..1215e4e
--- /dev/null
@@ -0,0 +1,162 @@
+#include <iostream>
+#include <iomanip>
+
+#include "jansson.hpp"
+
+#define ASSERT_OP(lhs, rhs, op, m)                                      \
+    do {                                                                \
+        if(!((lhs) op (rhs))) {                                         \
+            std::cerr << std::boolalpha;                                \
+            std::cerr << __FILE__ << '[' << __LINE__ << "]: ERROR: "    \
+                      << (m) << std::endl;                              \
+            std::cerr << "\ttest:   " << #lhs << ' ' << #op << ' '      \
+                      << #rhs << std::endl;                             \
+            std::cerr << "\tresult: " << (lhs) << ' ' << #op << ' '     \
+                      << (rhs) << std::endl;                            \
+            return 1;                                                   \
+        }                                                               \
+    } while(0)
+#define ASSERT_EQ(lhs, rhs, m) ASSERT_OP(lhs, rhs, ==, m)
+#define ASSERT_NE(lhs, rhs, m) ASSERT_OP(lhs, rhs, !=, m)
+#define ASSERT_TRUE(p, m) ASSERT_OP(p, true, ==, m)
+#define ASSERT_FALSE(p, m) ASSERT_OP(p, true, !=, m)
+
+int main() {
+    std::string top_srcdir = getenv("top_srcdir");
+    json::Value e1(json::load_file(top_srcdir + "/test/suites/api/test_cpp.json"));
+    json::Value e2(e1);
+    json::Value e3;
+    json::Value e4(json::loads("{\"foo\": true, \"bar\": \"test\"}"));
+
+    ASSERT_TRUE(e1.is_object(), "e1 is not an object");
+    ASSERT_TRUE(e2.is_object(), "e2 is not an object");
+    ASSERT_TRUE(e3.is_undefined(), "e3 has a defined value");
+    ASSERT_TRUE(e4.is_object(), "e4 is not an object");
+
+    ASSERT_EQ(e1.size(), 1, "e1 has too many properties");
+    ASSERT_EQ(e2.size(), 1, "e2 has too many properties");
+    ASSERT_EQ(e4.size(), 2, "e4 does not have 2 elements");
+
+    ASSERT_TRUE(e1.get("web-app").is_object(), "e1[0].web-app is not an object");
+    ASSERT_EQ(e1.get("web-app").get("servlet").at(0).get("servlet-class").as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value");
+    ASSERT_EQ(e1["web-app"]["servlet"][0]["servlet-class"].as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value");
+
+    ASSERT_EQ(e4["foo"].as_boolean(), true, "property has incorrect value");
+
+    json::Iterator i(e1.get("web-app"));
+    ASSERT_EQ(i.key(), "taglib", "first iterator result has incorrect key");
+    i.next();
+    ASSERT_EQ(i.key(), "servlet", "first iterator result has incorrect key");
+    i.next();
+    ASSERT_EQ(i.key(), "servlet-mapping", "first iterator result has incorrect key");
+    i.next();
+    ASSERT_FALSE(i.valid(), "iterator has more values than expected");
+
+    json::Value e5(json::Value(12.34));
+    ASSERT_TRUE(e5.is_number(), "e5 is not a number after assignment");
+    ASSERT_EQ(e5.as_real(), 12.34, "e5 has incorrect value after assignment");
+
+    json::Value e6(json::Value(true));
+    ASSERT_TRUE(e6.is_boolean(), "e6 is not a boolean after assignment");
+    ASSERT_EQ(e6.as_boolean(), true, "e6 has incorrect value after assignment");
+
+    json::Value e7(json::Value("foobar"));
+    ASSERT_TRUE(e7.is_string(), "e7 is not a string after assignment");
+    ASSERT_EQ(e7.as_string(), "foobar", "e7 has incorrect value after assignment");
+
+    json::Value e8(json::object());
+    ASSERT_TRUE(e8.is_object(), "e8 is not an object after assignment");
+
+    json::Value e9(json::null());
+    ASSERT_TRUE(e9.is_null(), "e9 is not null after assignment");
+
+    json::Value e10(json::array());
+    ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment");
+
+    e10.set_at(0, json::Value("foobar"));
+    ASSERT_EQ(e10.size(), 1, "e10 has incorrect number of elements after assignment");
+    ASSERT_EQ(e10[0].as_string(), "foobar", "e10[0] has incorrect value after assignment");
+
+    e10.set_at(1, json::Value("foobar"));
+    ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment");
+    ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment");
+    ASSERT_EQ(e10[1].as_string(), "foobar", "e10[0] has incorrect value after assignment");
+
+    e10.set_at(0, json::Value("barfoo"));
+    ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment");
+    ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment");
+    ASSERT_EQ(e10[0].as_string(), "barfoo", "e10[0] has incorrect value after assignment");
+
+    e10.set_at(100, json::null());
+    ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment");
+    ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment");
+
+    e10.insert_at(1, json::Value("new"));
+    ASSERT_EQ(e10.size(), 3, "e10 has incorrect size after insert");
+    ASSERT_EQ(e10[1].as_string(), "new", "e10[1] has incorrect value after insert");
+    ASSERT_EQ(e10[2].as_string(), "foobar", "e10[2] has incorrect value after insert");
+
+    e10.del_at(0);
+    ASSERT_EQ(e10.size(), 2, "e10 has incorrect size after delete");
+    ASSERT_EQ(e10[1].as_string(), "foobar", "e10[1] has incorrect value after delete");
+
+    e10.clear();
+    ASSERT_EQ(e10.size(), 0, "e10 has incorrect number of elements after clear");
+
+    json::Value e11(json::object());
+    ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment");
+
+    e11.set_key("foo", json::Value("test"));
+    ASSERT_EQ(e11.size(), 1, "e11 has incorrect number of properties after assignment");
+    ASSERT_EQ(e11["foo"].as_string(), "test", "e11.foo has incorrect value after assignment");
+
+    e11.set_key("foo", json::Value("again"));
+    ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment");
+    ASSERT_EQ(e11.size(), 1, "e11 has incorrect number of properties after assignment");
+    ASSERT_EQ(e11["foo"].as_string(), "again", "e11.foo has incorrect value after assignment");
+
+    e11.set_key("bar", json::Value("test"));
+    ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment");
+    ASSERT_EQ(e11.size(), 2, "e11 has incorrect number of properties after assignment");
+    ASSERT_EQ(e11["bar"].as_string(), "test", "e11.foo has incorrect value after assignment");
+
+    e11.clear();
+    ASSERT_EQ(e11.size(), 0, "e11 has incorrect number of properties after clear");
+
+    json::Value e12(json::object());
+    e12.set_key("foo", json::Value("test"));
+    e12.set_key("bar", json::Value(3));
+    char* out_cstr = e12.dumps(0);
+    std::string out(out_cstr);
+    free(out_cstr);
+    ASSERT_EQ(out, "{\"bar\": 3, \"foo\": \"test\"}", "object did not serialize as expected");
+
+    std::istringstream instr(out);
+    instr >> e12;
+    ASSERT_TRUE(e12.is_object(), "e12 is not an object after stream read");
+    ASSERT_EQ(e12.size(), 2, "e12 has wrong size after stream read");
+    ASSERT_EQ(e12.get("bar").as_integer(), 3, "e12.bar has incorrect value after stream read");
+    ASSERT_EQ(e12.get("foo").as_string(), "test", "ee12.test has incorrect value after stream read");
+
+    std::ostringstream outstr;
+    outstr << e12;
+    ASSERT_EQ(instr.str(), "{\"bar\": 3, \"foo\": \"test\"}", "object did not serialize as expected");
+
+    const json::Value e13(e12);
+    ASSERT_EQ(e13["bar"].as_integer(), 3, "e13.bar has incorrect value after copy");
+
+    json::Value e14(json::object());
+    ASSERT_TRUE(e14.is_object(), "e14 is not an object after construction");
+    e14.set_key("foo", json::object());
+    ASSERT_TRUE(e14["foo"].is_object(), "e14.foo is not an object after assignment");
+    e14["foo"]["bar"] = json::Value(42);
+    ASSERT_EQ(e14["foo"]["bar"].as_integer(), 42, "e14.foo.bar has incorrect value after assignment");
+
+    json::Value e15(json::array());
+    ASSERT_TRUE(e15.is_array(), "e15 is not an array after construction");
+    e15.set_at(0, json::Value(42));
+    ASSERT_EQ(e15[0].as_integer(), 42, "e15[0] has incorrect value after assignment");
+    e15[0] = json::Value("foo");
+    ASSERT_EQ(e15[0].as_string(), "foo", "e15[0] has incorrecy value after assignment");
+    return 0;
+}
diff --git a/test/suites/api/test_cpp.json b/test/suites/api/test_cpp.json
new file mode 100644 (file)
index 0000000..d31a395
--- /dev/null
@@ -0,0 +1,88 @@
+{"web-app": {
+  "servlet": [
+    {
+      "servlet-name": "cofaxCDS",
+      "servlet-class": "org.cofax.cds.CDSServlet",
+      "init-param": {
+        "configGlossary:installationAt": "Philadelphia, PA",
+        "configGlossary:adminEmail": "ksm@pobox.com",
+        "configGlossary:poweredBy": "Cofax",
+        "configGlossary:poweredByIcon": "/images/cofax.gif",
+        "configGlossary:staticPath": "/content/static",
+        "templateProcessorClass": "org.cofax.WysiwygTemplate",
+        "templateLoaderClass": "org.cofax.FilesTemplateLoader",
+        "templatePath": "templates",
+        "templateOverridePath": "",
+        "defaultListTemplate": "listTemplate.htm",
+        "defaultFileTemplate": "articleTemplate.htm",
+        "useJSP": false,
+        "jspListTemplate": "listTemplate.jsp",
+        "jspFileTemplate": "articleTemplate.jsp",
+        "cachePackageTagsTrack": 200,
+        "cachePackageTagsStore": 200,
+        "cachePackageTagsRefresh": 60,
+        "cacheTemplatesTrack": 100,
+        "cacheTemplatesStore": 50,
+        "cacheTemplatesRefresh": 15,
+        "cachePagesTrack": 200,
+        "cachePagesStore": 100,
+        "cachePagesRefresh": 10,
+        "cachePagesDirtyRead": 10,
+        "searchEngineListTemplate": "forSearchEnginesList.htm",
+        "searchEngineFileTemplate": "forSearchEngines.htm",
+        "searchEngineRobotsDb": "WEB-INF/robots.db",
+        "useDataStore": true,
+        "dataStoreClass": "org.cofax.SqlDataStore",
+        "redirectionClass": "org.cofax.SqlRedirection",
+        "dataStoreName": "cofax",
+        "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
+        "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
+        "dataStoreUser": "sa",
+        "dataStorePassword": "dataStoreTestQuery",
+        "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
+        "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
+        "dataStoreInitConns": 10,
+        "dataStoreMaxConns": 100,
+        "dataStoreConnUsageLimit": 100,
+        "dataStoreLogLevel": "debug",
+        "maxUrlLength": 500}},
+    {
+      "servlet-name": "cofaxEmail",
+      "servlet-class": "org.cofax.cds.EmailServlet",
+      "init-param": {
+      "mailHost": "mail1",
+      "mailHostOverride": "mail2"}},
+    {
+      "servlet-name": "cofaxAdmin",
+      "servlet-class": "org.cofax.cds.AdminServlet"},
+
+    {
+      "servlet-name": "fileServlet",
+      "servlet-class": "org.cofax.cds.FileServlet"},
+    {
+      "servlet-name": "cofaxTools",
+      "servlet-class": "org.cofax.cms.CofaxToolsServlet",
+      "init-param": {
+        "templatePath": "toolstemplates/",
+        "log": 1,
+        "logLocation": "/usr/local/tomcat/logs/CofaxTools.log",
+        "logMaxSize": "",
+        "dataLog": 1,
+        "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log",
+        "dataLogMaxSize": "",
+        "removePageCache": "/content/admin/remove?cache=pages&id=",
+        "removeTemplateCache": "/content/admin/remove?cache=templates&id=",
+        "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder",
+        "lookInContext": 1,
+        "adminGroupID": 4,
+        "betaServer": true}}],
+  "servlet-mapping": {
+    "cofaxCDS": "/",
+    "cofaxEmail": "/cofaxutil/aemail/*",
+    "cofaxAdmin": "/admin/*",
+    "fileServlet": "/static/*",
+    "cofaxTools": "/tools/*"},
+
+  "taglib": {
+    "taglib-uri": "cofax.tld",
+    "taglib-location": "/WEB-INF/tlds/cofax.tld"}}}
index 67a93c8..90370e5 100644 (file)
@@ -258,6 +258,36 @@ static void test_iterators()
     if(json_object_iter_next(object, iter) != NULL)
         fail("able to iterate over the end");
 
+    if(json_object_iter_at(object, "foo"))
+        fail("json_object_iter_at() succeeds for non-existent key");
+
+    iter = json_object_iter_at(object, "b");
+    if(!iter)
+        fail("json_object_iter_at() fails for an existing key");
+
+    if(strcmp(json_object_iter_key(iter), "b"))
+        fail("iterating failed: wrong key");
+    if(json_object_iter_value(iter) != bar)
+        fail("iterating failed: wrong value");
+
+    iter = json_object_iter_next(object, iter);
+    if(!iter)
+        fail("unable to increment iterator");
+    if(strcmp(json_object_iter_key(iter), "c"))
+        fail("iterating failed: wrong key");
+    if(json_object_iter_value(iter) != baz)
+        fail("iterating failed: wrong value");
+
+    if(json_object_iter_set(object, iter, bar))
+        fail("unable to set value at iterator");
+
+    if(strcmp(json_object_iter_key(iter), "c"))
+        fail("json_object_iter_key() fails after json_object_iter_set()");
+    if(json_object_iter_value(iter) != bar)
+        fail("json_object_iter_value() fails after json_object_iter_set()");
+    if(json_object_get(object, "c") != bar)
+        fail("json_object_get() fails after json_object_iter_set()");
+
     json_decref(object);
     json_decref(foo);
     json_decref(bar);
@@ -372,6 +402,41 @@ static void test_misc()
     json_decref(object);
 }
 
+static void test_preserve_order()
+{
+    json_t *object;
+    char *result;
+
+    const char *expected = "{\"foobar\": 1, \"bazquux\": 6, \"lorem ipsum\": 3, \"sit amet\": 5, \"helicopter\": 7}";
+
+    object = json_object();
+
+    json_object_set_new(object, "foobar", json_integer(1));
+    json_object_set_new(object, "bazquux", json_integer(2));
+    json_object_set_new(object, "lorem ipsum", json_integer(3));
+    json_object_set_new(object, "dolor", json_integer(4));
+    json_object_set_new(object, "sit amet", json_integer(5));
+
+    /* changing a value should preserve the order */
+    json_object_set_new(object, "bazquux", json_integer(6));
+
+    /* deletion shouldn't change the order of others */
+    json_object_del(object, "dolor");
+
+    /* add a new item just to make sure */
+    json_object_set_new(object, "helicopter", json_integer(7));
+
+    result = json_dumps(object, JSON_PRESERVE_ORDER);
+
+    if(strcmp(expected, result) != 0) {
+        fprintf(stderr, "%s != %s", expected, result);
+        fail("JSON_PRESERVE_ORDER doesn't work");
+    }
+
+    free(result);
+    json_decref(object);
+}
+
 int main()
 {
     test_misc();
@@ -380,6 +445,7 @@ int main()
     test_circular();
     test_set_nocheck();
     test_iterators();
+    test_preserve_order();
 
     return 0;
 }
diff --git a/test/suites/encoding-flags/preserve-order/env b/test/suites/encoding-flags/preserve-order/env
new file mode 100644 (file)
index 0000000..ce3582d
--- /dev/null
@@ -0,0 +1 @@
+export JSON_PRESERVE_ORDER=1
diff --git a/test/suites/encoding-flags/preserve-order/input b/test/suites/encoding-flags/preserve-order/input
new file mode 100644 (file)
index 0000000..27bcf18
--- /dev/null
@@ -0,0 +1 @@
+{"foo": 1, "bar": 2, "asdf": 3, "deadbeef": 4, "badc0ffee": 5, "qwerty": 6}
diff --git a/test/suites/encoding-flags/preserve-order/output b/test/suites/encoding-flags/preserve-order/output
new file mode 100644 (file)
index 0000000..7a443f6
--- /dev/null
@@ -0,0 +1 @@
+{"foo": 1, "bar": 2, "asdf": 3, "deadbeef": 4, "badc0ffee": 5, "qwerty": 6}
\ No newline at end of file