pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_JSON.h
00001 /*
00002  -------------------------------------------------------------------------------
00003  This file is part of the Plink, Plonk, Plank libraries
00004   by Martin Robinson
00005  
00006  http://code.google.com/p/pl-nk/
00007  
00008  Copyright University of the West of England, Bristol 2011-14
00009  All rights reserved.
00010  
00011  Redistribution and use in source and binary forms, with or without
00012  modification, are permitted provided that the following conditions are met:
00013  
00014  * Redistributions of source code must retain the above copyright
00015    notice, this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright
00017    notice, this list of conditions and the following disclaimer in the
00018    documentation and/or other materials provided with the distribution.
00019  * Neither the name of University of the West of England, Bristol nor 
00020    the names of its contributors may be used to endorse or promote products
00021    derived from this software without specific prior written permission.
00022  
00023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026  DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 
00027  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00028  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00029  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00030  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00031  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00032  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00033  
00034  This software makes use of third party libraries. For more information see:
00035  doc/license.txt included in the distribution.
00036  -------------------------------------------------------------------------------
00037  */
00038 
00039 #ifndef PLONK_JSON_H
00040 #define PLONK_JSON_H
00041 
00042 
00043 class JSON
00044 {
00045 public:
00046     inline JSON() throw() : json (0) { }
00047     inline JSON (const char value) throw() : json (pl_JSON_Int (LongLong (value))) { }
00048     inline JSON (const int value) throw() : json (pl_JSON_Int (LongLong (value))) { }
00049     inline JSON (const Long value) throw() : json (pl_JSON_Int (LongLong (value))) { }
00050     inline JSON (const LongLong value) throw() : json (pl_JSON_Int (value)) { }
00051     inline JSON (const float value) throw() : json (pl_JSON_Float (value)) { }
00052     inline JSON (const double value) throw() : json (pl_JSON_Double (value)) { }
00053     inline JSON (LongLongArray const& values) throw() : json (pl_JSON_IntArray (values.getArray(), values.length())) { }
00054     inline JSON (FloatArray const& values) throw() : json (pl_JSON_FloatArray (values.getArray(), values.length())) { }
00055     inline JSON (DoubleArray const& values) throw() : json (pl_JSON_DoubleArray (values.getArray(), values.length())) { }
00056     inline JSON (Text const& text) throw() : json (pl_JSON_String (text.getArray())) { }
00057     inline JSON (const char* text) throw() : json (pl_JSON_String (text)) { }
00058 
00059     inline JSON (const Long value, const bool useBinary) throw()
00060     : json (useBinary ? pl_JSON_IntBinary (LongLong (value)) : pl_JSON_Int (LongLong (value)))
00061     { }
00062 
00063     inline JSON (const LongLong value, const bool useBinary) throw()
00064     : json (useBinary ? pl_JSON_IntBinary (value) : pl_JSON_Int (value))
00065     { }
00066     
00067     inline JSON (const float value, const bool useBinary) throw()
00068     : json (useBinary ? pl_JSON_FloatBinary (value) : pl_JSON_Float (value))
00069     { }
00070     
00071     inline JSON (const double value, const bool useBinary) throw()
00072     : json (useBinary ? pl_JSON_DoubleBinary (value) : pl_JSON_Double (value))
00073     { }
00074     
00075     inline JSON (LongLongArray const& values, const bool useBinary) throw()
00076     : json (useBinary ? pl_JSON_IntArrayBinary (values.getArray(), values.length()) : pl_JSON_IntArray (values.getArray(), values.length()))
00077     { }
00078     
00079     inline JSON (FloatArray const& values, const bool useBinary) throw()
00080     : json (useBinary ? pl_JSON_FloatArrayBinary (values.getArray(), values.length()) : pl_JSON_FloatArray (values.getArray(), values.length()))
00081     { }
00082     
00083     inline JSON (DoubleArray const& values, const bool useBinary) throw()
00084     : json (useBinary ? pl_JSON_DoubleArrayBinary (values.getArray(), values.length()) : pl_JSON_DoubleArray (values.getArray(), values.length()))
00085     { }
00086 
00087     inline JSON (BinaryFile const& file) throw()
00088     : json (0)
00089     {
00090         if (file.canRead())
00091         {
00092             json = pl_JSON_FromFile (file->getInternal()->getPeerRef());
00093         }
00094     }
00095     
00096     static JSON array() throw() { return JSON (pl_JSON_Array()); }
00097     static JSON object() throw() { return JSON (pl_JSON_Object()); }
00098     static JSON null() throw() { return JSON (pl_JSON_Null()); }
00099     
00100     inline JSON (JSON const& other) throw()
00101     : json (pl_JSON_IncrementRefCount (other.json))
00102     {
00103     }
00104     
00105     inline JSON& operator= (JSON const& other) throw()
00106     {
00107         if (this != &other)
00108         {
00109             PlankJSONRef temp = pl_JSON_IncrementRefCount (other.json);
00110             pl_JSON_DecrementRefCount (json);
00111             json = temp;
00112         }
00113         
00114         return *this;
00115     }
00116 
00117     inline ~JSON()
00118     {
00119         pl_JSON_DecrementRefCount (json);
00120     }
00121     
00122     inline ResultCode toFile (BinaryFile const& file) throw()
00123     {        
00124         return file.canWrite() ? pl_JSON_WriteToFile (json, file.getInternal()->getPeerRef(), PLANK_JSON_DEFAULTFLAGS) : PlankResult_FileWriteError;
00125     }
00126         
00127     inline ResultCode toFile (BinaryFile const& file, const int flags) throw()
00128     {
00129         return file.canWrite() ? pl_JSON_WriteToFile (json, file.getInternal()->getPeerRef(), flags) : PlankResult_FileWriteError;
00130     }
00131 
00132     
00133     inline bool isEmpty() const throw() { return json == 0; }
00134     inline bool isInt() const throw() { return pl_JSON_IsInt (json); }
00135     inline bool isFloat() const throw() { return pl_JSON_IsFloat (json); }
00136     inline bool isDouble() const throw() { return pl_JSON_IsDouble (json); }
00137     inline bool isBool() const throw() { return pl_JSON_IsBool (json); }
00138     inline bool isNull() const throw() { return pl_JSON_IsNull (json); }
00139     inline bool isArray() const throw() { return pl_JSON_IsArray (json); }
00140     inline bool isObject() const throw() { return pl_JSON_IsObject (json); }
00141     inline bool isString() const throw() { return pl_JSON_IsString (json); }
00142     inline bool isIntArrayEncoded() const throw() { return pl_JSON_IsIntArrayEncoded (json); }
00143     inline bool isFloatArrayEncoded() const throw() { return pl_JSON_IsFloatArrayEncoded (json); }
00144     inline bool isDoubleArrayEncoded() const throw() { return pl_JSON_IsDoubleArrayEncoded (json); }
00145     inline bool isError() const throw() { return pl_JSON_IsError (json); }
00146 
00147     inline bool operator== (JSON const& other) const throw() { return json == other.json; }
00148     inline bool operator!= (JSON const& other) const throw() { return json != other.json; }
00149 
00150     operator int () const throw() { return int (pl_JSON_IntGet (json)); }
00151     operator LongLong () const throw() { return pl_JSON_IntGet (json); }
00152     operator UnsignedLongLong () const throw() { return UnsignedLongLong (pl_JSON_IntGet (json)); }
00153     operator float () const throw() { return pl_JSON_FloatGet (json); }
00154     operator double () const throw() { return pl_JSON_DoubleGet (json); }
00155     operator Text () const throw() { return getText(); }
00156     operator const char* () const throw() { return pl_JSON_StringGet (json); }
00157        
00158     LongLong getInt() const throw() { return pl_JSON_IntGet (json); }
00159     UnsignedLongLong getUnsignedInt() const throw() { return PlankUI (pl_JSON_IntGet (json)); }
00160     float getFloat() const throw() { return pl_JSON_FloatGet (json); }
00161     double getDouble() const throw() { return pl_JSON_DoubleGet (json); }
00162     Text getText() const throw() { const char* string = pl_JSON_StringGet (json); return string ? string : ""; }
00163     const char* getCString() const throw() { return pl_JSON_StringGet (json); }
00164                 
00165     operator LongLongArray () const throw() { return getIntArray();  }
00166     operator FloatArray () const throw() { return getFloatArray(); }
00167     operator DoubleArray () const throw() { return getDoubleArray(); }
00168 
00169     LongLongArray getIntArray() const throw()
00170     {
00171         if (isIntArrayEncoded() || isArray())
00172         {
00173             PlankDynamicArray array;
00174             pl_DynamicArray_Init (&array);
00175             
00176             if (pl_JSON_IntArrayGet (json, &array) == PlankResult_OK)
00177                 return LongLongArray::withArray((int)pl_DynamicArray_GetSize (&array), static_cast<LongLong*> (pl_DynamicArray_GetArray (&array)));
00178             else
00179                 pl_DynamicArray_DeInit (&array);
00180         }
00181         
00182         return IntArray::getNull();
00183     }
00184     
00185     FloatArray getFloatArray() const throw()
00186     {
00187         if (isFloatArrayEncoded() || isArray())
00188         {
00189             PlankDynamicArray array;
00190             pl_DynamicArray_Init (&array);
00191             
00192             if (pl_JSON_FloatArrayGet (json, &array) == PlankResult_OK)
00193                 return FloatArray::withArray((int)pl_DynamicArray_GetSize (&array), static_cast<float*> (pl_DynamicArray_GetArray (&array)));
00194             else
00195                 pl_DynamicArray_DeInit (&array);
00196         }
00197         
00198         return FloatArray::getNull();
00199     }
00200     
00201     DoubleArray getDoubleArray() const throw()
00202     {
00203         if (isDoubleArrayEncoded() || isArray())
00204         {
00205             PlankDynamicArray array;
00206             pl_DynamicArray_Init (&array);
00207             
00208             if (pl_JSON_DoubleArrayGet (json, &array) == PlankResult_OK)
00209                 return DoubleArray::withArray((int)pl_DynamicArray_GetSize (&array), static_cast<double*> (pl_DynamicArray_GetArray (&array)));
00210             else
00211                 pl_DynamicArray_DeInit (&array);
00212         }
00213         
00214         return DoubleArray::getNull();
00215     }
00216 
00217     inline JSON operator[] (const int index) const throw()
00218     {
00219         return JSON (pl_JSON_ArrayAt (json, index));
00220     }
00221 
00222     inline JSON operator[] (const Long index) const throw()
00223     {
00224         return JSON (pl_JSON_ArrayAt (json, index));
00225     }
00226 
00227     inline JSON operator[] (const char* key) const throw()
00228     {
00229         return JSON (pl_JSON_ObjectAtKey (json, key));
00230     }
00231 
00232     inline JSON& put (const Long index, JSON const& item) throw()
00233     {
00234         if (isArray())
00235             pl_JSON_ArrayPut (json, index, pl_JSON_IncrementRefCount (item.json));
00236         
00237         return *this;
00238     }
00239         
00240     inline JSON& add (JSON const& item) throw()
00241     {
00242         if (isArray())
00243             pl_JSON_ArrayAppend (json, pl_JSON_IncrementRefCount (item.json));
00244         
00245         return *this;
00246     }
00247 
00248     inline JSON& add (const char* key, JSON const& item) throw()
00249     {
00250         if (isObject())
00251             pl_JSON_ObjectPutKey (json, key, pl_JSON_IncrementRefCount (item.json));
00252         
00253         return *this;
00254     }
00255 
00256     inline Long length() const throw()
00257     {
00258         if (isArray())
00259             return pl_JSON_ArrayGetSize (json);
00260         else if (isObject())
00261             return pl_JSON_ObjectGetSize (json);
00262         else if (isString())
00263             return strlen (pl_JSON_StringGet (json));
00264         else if (!json || isNull())
00265             return 0;
00266         else
00267             return 1;
00268     }
00269   
00270     static JSON versionString (const UnsignedChar ex, const UnsignedChar major, const UnsignedChar minor, const UnsignedChar micro) throw()
00271     {
00272         return pl_JSON_VersionString (ex, major, minor, micro);
00273     }
00274         
00275     static UnsignedInt versionCode (const UnsignedChar ex, const UnsignedChar major, const UnsignedChar minor, const UnsignedChar micro) throw()
00276     {
00277         return pl_JSON_VersionCode (ex, major, minor, micro);
00278     }
00279         
00280     UnsignedInt getVersion() const throw()
00281     {
00282         return pl_JSON_ObjectGetVersion (json);
00283     }
00284         
00285     void setType (const char* type) throw()
00286     {
00287         pl_JSON_ObjectSetType (json, type);
00288     }
00289         
00290     void setVersionString (const UnsignedChar ex, const UnsignedChar major, const UnsignedChar minor, const UnsignedChar micro) throw()
00291     {
00292         pl_JSON_ObjectSetVersionString (json, ex, major, minor, micro);
00293     }
00294         
00295     void setVersionCode (const UnsignedChar ex, const UnsignedChar major, const UnsignedChar minor, const UnsignedChar micro) throw()
00296     {
00297         pl_JSON_ObjectSetVersionCode (json, ex, major, minor, micro);
00298     }
00299         
00300     bool isObjectType (const char* type) const throw()
00301     {
00302         return pl_JSON_IsObjectType (json, type);
00303     }
00304         
00305     Text dump() const throw()
00306     {
00307         return json_dumps ((json_t*)json, 0);
00308     }
00309         
00310     PlankJSONRef getInternal() throw() { return json; }
00311     const PlankJSONRef getInternal() const throw() { return json; }
00312         
00313 private:
00314     inline JSON (PlankJSONRef other) throw()
00315     : json (pl_JSON_IncrementRefCount (other))
00316     {
00317     }
00318         
00319     PlankJSONRef json;
00320 };
00321 
00322 
00323 #endif // PLONK_JSON_H
 All Classes Functions Typedefs Enumerations Enumerator Properties