Browse Source

add type checking methods to json arrays and objects

Kolja Strohm 2 years ago
parent
commit
59ff33c6bf
3 changed files with 95 additions and 79 deletions
  1. 1 2
      Array.h
  2. 43 28
      JSON.cpp
  3. 51 49
      JSON.h

+ 1 - 2
Array.h

@@ -69,7 +69,7 @@ namespace Framework
 #pragma warning( once : 26495 )
     };
 #else
-};
+    };
 #endif
 
 
@@ -539,7 +539,6 @@ namespace Framework
             int anz = arr.getEintragAnzahl();
             for( int i = 0; i < anz; i++ )
                 add( arr.get( i ) );
-            ref = 1;
         }
 
         //! Leert und löscht die Linked List 

+ 43 - 28
JSON.cpp

@@ -102,7 +102,7 @@ JSONArray::JSONArray( Text string )
         while( string.getLength() )
         {
             int end = Parser::findObjectEndInArray( string );
-            Text *objStr = string.getTeilText( 0, end );
+            Text* objStr = string.getTeilText( 0, end );
             string.remove( 0, end + 1 );
             array->add( Parser::getValue( *objStr ) );
             objStr->release();
@@ -110,10 +110,10 @@ JSONArray::JSONArray( Text string )
     }
 }
 
-JSONArray::JSONArray( const JSONArray &arr )
+JSONArray::JSONArray( const JSONArray& arr )
     : JSONValue( JSONType::ARRAY )
 {
-    array = dynamic_cast<RCArray<JSONValue> *>( arr.array->getThis() );
+    array = dynamic_cast<RCArray<JSONValue> *>(arr.array->getThis());
 }
 
 JSONArray::~JSONArray()
@@ -121,19 +121,19 @@ JSONArray::~JSONArray()
     array->release();
 }
 
-JSONArray &JSONArray::operator=( const JSONArray &arr )
+JSONArray& JSONArray::operator=( const JSONArray& arr )
 {
     array->release();
-    array = dynamic_cast<RCArray<JSONValue> *>( arr.array->getThis() );
+    array = dynamic_cast<RCArray<JSONValue> *>(arr.array->getThis());
     return *this;
 }
 
-void JSONArray::addValue( JSONValue *value )
+void JSONArray::addValue( JSONValue* value )
 {
     array->add( value );
 }
 
-JSONValue *JSONArray::getValue( int i ) const
+JSONValue* JSONArray::getValue( int i ) const
 {
     return array->get( i );
 }
@@ -143,6 +143,11 @@ int JSONArray::getLength() const
     return array->getEintragAnzahl();
 }
 
+bool JSONArray::isValueOfType( int i, JSONType type ) const
+{
+    return i >= 0 && i < array->getEintragAnzahl() && array->z( i )->getType() == type;
+}
+
 Text JSONArray::toString() const
 {
     Text str = "[";
@@ -177,12 +182,12 @@ JSONObject::JSONObject( Text string )
         while( string.getLength() )
         {
             int endField = Parser::findFieldEndInObject( string );
-            Text *fieldName = string.getTeilText( 0, endField );
+            Text* fieldName = string.getTeilText( 0, endField );
             string.remove( 0, endField + 1 );
             fieldName->remove( 0, 1 );
             fieldName->remove( fieldName->getLength() - 1, fieldName->getLength() );
             int endValue = Parser::findValueEndInObject( string );
-            Text *value = string.getTeilText( 0, endValue );
+            Text* value = string.getTeilText( 0, endValue );
             string.remove( 0, endValue + 1 );
             fields->add( Text( fieldName->getText() ) );
             values->add( Parser::getValue( *value ) );
@@ -192,11 +197,11 @@ JSONObject::JSONObject( Text string )
     }
 }
 
-JSONObject::JSONObject( const JSONObject &obj )
+JSONObject::JSONObject( const JSONObject& obj )
     : JSONValue( JSONType::OBJECT )
 {
-    fields = dynamic_cast<Array<Text> *>( obj.fields->getThis() );
-    values = dynamic_cast<RCArray<JSONValue> *>( obj.values->getThis() );
+    fields = dynamic_cast<Array<Text> *>(obj.fields->getThis());
+    values = dynamic_cast<RCArray<JSONValue> *>(obj.values->getThis());
 }
 
 JSONObject::~JSONObject()
@@ -206,17 +211,17 @@ JSONObject::~JSONObject()
 }
 
 
-JSONObject &JSONObject::operator=( const JSONObject &obj )
+JSONObject& JSONObject::operator=( const JSONObject& obj )
 {
     fields->release();
     values->release();
-    fields = dynamic_cast<Array<Text> *>( obj.fields->getThis() );
-    values = dynamic_cast<RCArray<JSONValue> *>( obj.values->getThis() );
+    fields = dynamic_cast<Array<Text> *>(obj.fields->getThis());
+    values = dynamic_cast<RCArray<JSONValue> *>(obj.values->getThis());
     return *this;
 }
 
 
-bool JSONObject::addValue( Text field, JSONValue *value )
+bool JSONObject::addValue( Text field, JSONValue* value )
 {
     if( hasValue( field ) )
         return 0;
@@ -249,7 +254,7 @@ bool JSONObject::hasValue( Text field )
     return 0;
 }
 
-JSONValue *JSONObject::getValue( Text field )
+JSONValue* JSONObject::getValue( Text field )
 {
     for( int i = 0; i < fields->getEintragAnzahl(); i++ )
     {
@@ -264,7 +269,7 @@ Iterator< Text > JSONObject::getFields()
     return fields->begin();
 }
 
-Iterator< JSONValue * > JSONObject::getValues()
+Iterator< JSONValue* > JSONObject::getValues()
 {
     return values->begin();
 }
@@ -274,6 +279,16 @@ int JSONObject::getFieldCount() const
     return fields->getEintragAnzahl();
 }
 
+bool JSONObject::isValueOfType( Text field, JSONType type ) const
+{
+    for( int i = 0; i < fields->getEintragAnzahl(); i++ )
+    {
+        if( fields->get( i ).istGleich( field ) )
+            return values->z( i )->getType() == type;
+    }
+    return 0;
+}
+
 Text JSONObject::toString() const
 {
     Text str = "{";
@@ -291,27 +306,27 @@ Text JSONObject::toString() const
     return str;
 }
 
-JSONValue *JSON::loadJSONFromFile( Text path )
+JSONValue* JSON::loadJSONFromFile( Text path )
 {
     Datei d;
     d.setDatei( path );
     d.open( Datei::Style::lesen );
     int size = (int)d.getSize();
-    char *buffer = new char[ size + 1 ];
+    char* buffer = new char[ size + 1 ];
     buffer[ size ] = 0;
     d.lese( buffer, size );
     d.close();
-    JSONValue *result = Parser::getValue( buffer );
+    JSONValue* result = Parser::getValue( buffer );
     delete[] buffer;
     return result;
 }
 
-int Parser::findObjectEndInArray( const char *str )
+int Parser::findObjectEndInArray( const char* str )
 {
     return findValueEndInObject( str );
 }
 
-Text Parser::removeWhitespace( const char *str )
+Text Parser::removeWhitespace( const char* str )
 {
     int wsc = 0;
     int i = 0;
@@ -385,7 +400,7 @@ Text Parser::removeWhitespace( const char *str )
     return ret;
 }
 
-JSONValue *Parser::getValue( const char *str )
+JSONValue* Parser::getValue( const char* str )
 {
     Text string = Parser::removeWhitespace( str );
     if( string.istGleich( "true" ) )
@@ -407,15 +422,15 @@ JSONValue *Parser::getValue( const char *str )
     if( string.anzahlVon( '.' ) == 1 )
     {
         bool isNumber = 1;
-        for( char *c = ( *string.getText() == '-' ) ? string.getText() + 1 : string.getText(); *c; c++ )
-            isNumber &= ( *c >= '0' && *c <= '9' ) || *c == '.';
+        for( char* c = (*string.getText() == '-') ? string.getText() + 1 : string.getText(); *c; c++ )
+            isNumber &= (*c >= '0' && *c <= '9') || *c == '.';
         if( isNumber )
             return new JSONNumber( string );
     }
     return new JSONValue();
 }
 
-int Parser::findFieldEndInObject( const char *str )
+int Parser::findFieldEndInObject( const char* str )
 {
     int i = 0;
     bool esc = 0;
@@ -470,7 +485,7 @@ int Parser::findFieldEndInObject( const char *str )
     return i;
 }
 
-int Parser::findValueEndInObject( const char *str )
+int Parser::findValueEndInObject( const char* str )
 {
     int i = 0;
     bool esc = 0;

+ 51 - 49
JSON.h

@@ -27,13 +27,13 @@ namespace Framework
             JSONType type;
 
         protected:
-            __declspec( dllexport ) JSONValue( JSONType type );
+            __declspec(dllexport) JSONValue( JSONType type );
 
         public:
-            __declspec( dllexport ) JSONValue();
-            __declspec( dllexport ) virtual ~JSONValue();
-            __declspec( dllexport ) JSONType getType() const;
-            __declspec( dllexport ) virtual Text toString() const;
+            __declspec(dllexport) JSONValue();
+            __declspec(dllexport) virtual ~JSONValue();
+            __declspec(dllexport) JSONType getType() const;
+            __declspec(dllexport) virtual Text toString() const;
         };
 
         class JSONBool : public JSONValue
@@ -41,11 +41,11 @@ namespace Framework
         private:
             bool b;
 
-        public: 
-            __declspec( dllexport ) JSONBool( bool b );
+        public:
+            __declspec(dllexport) JSONBool( bool b );
 
-            __declspec( dllexport ) bool getBool() const;
-            __declspec( dllexport ) Text toString() const override;
+            __declspec(dllexport) bool getBool() const;
+            __declspec(dllexport) Text toString() const override;
         };
 
         class JSONNumber : public JSONValue
@@ -54,10 +54,10 @@ namespace Framework
             double number;
 
         public:
-            __declspec( dllexport ) JSONNumber( double num );
+            __declspec(dllexport) JSONNumber( double num );
 
-            __declspec( dllexport ) double getNumber() const;
-            __declspec( dllexport ) Text toString() const override;
+            __declspec(dllexport) double getNumber() const;
+            __declspec(dllexport) Text toString() const override;
         };
 
         class JSONString : public JSONValue
@@ -66,66 +66,68 @@ namespace Framework
             Text string;
 
         public:
-            __declspec( dllexport ) JSONString( Text string );
+            __declspec(dllexport) JSONString( Text string );
 
-            __declspec( dllexport ) Text getString() const;
-            __declspec( dllexport ) Text toString() const override;
+            __declspec(dllexport) Text getString() const;
+            __declspec(dllexport) Text toString() const override;
         };
 
         class JSONArray : public JSONValue
         {
         private:
-            RCArray< JSONValue > *array;
+            RCArray< JSONValue >* array;
 
         public:
-            __declspec( dllexport ) JSONArray();
-            __declspec( dllexport ) JSONArray( Text string );
-            __declspec( dllexport ) JSONArray( const JSONArray &arr );
-            __declspec( dllexport ) ~JSONArray();
+            __declspec(dllexport) JSONArray();
+            __declspec(dllexport) JSONArray( Text string );
+            __declspec(dllexport) JSONArray( const JSONArray& arr );
+            __declspec(dllexport) ~JSONArray();
 
-            __declspec( dllexport ) JSONArray &operator=( const JSONArray &arr );
+            __declspec(dllexport) JSONArray& operator=( const JSONArray& arr );
 
-            __declspec( dllexport ) void addValue( JSONValue *value );
-            __declspec( dllexport ) JSONValue *getValue( int i ) const;
-            __declspec( dllexport ) int getLength() const;
+            __declspec(dllexport) void addValue( JSONValue* value );
+            __declspec(dllexport) JSONValue* getValue( int i ) const;
+            __declspec(dllexport) int getLength() const;
+            __declspec(dllexport) bool isValueOfType( int i, JSONType type ) const;
 
-            __declspec( dllexport ) Text toString() const override;
+            __declspec(dllexport) Text toString() const override;
         };
 
         class JSONObject : public JSONValue
         {
         private:
-            Array< Text > *fields;
-            RCArray< JSONValue > *values;
+            Array< Text >* fields;
+            RCArray< JSONValue >* values;
 
         public:
-            __declspec( dllexport ) JSONObject();
-            __declspec( dllexport ) JSONObject( Text string );
-            __declspec( dllexport ) JSONObject( const JSONObject &obj );
-            __declspec( dllexport ) ~JSONObject();
-
-            __declspec( dllexport ) JSONObject &operator=( const JSONObject &obj );
-
-            __declspec( dllexport ) bool addValue( Text field, JSONValue *value );
-            __declspec( dllexport ) bool removeValue( Text field );
-            __declspec( dllexport ) bool hasValue( Text field );
-            __declspec( dllexport ) JSONValue *getValue( Text field );
-            __declspec( dllexport ) Iterator< Text > getFields();
-            __declspec( dllexport ) Iterator< JSONValue* > getValues();
-            __declspec( dllexport ) int getFieldCount() const;
-
-            __declspec( dllexport ) Text toString() const override;
+            __declspec(dllexport) JSONObject();
+            __declspec(dllexport) JSONObject( Text string );
+            __declspec(dllexport) JSONObject( const JSONObject& obj );
+            __declspec(dllexport) ~JSONObject();
+
+            __declspec(dllexport) JSONObject& operator=( const JSONObject& obj );
+
+            __declspec(dllexport) bool addValue( Text field, JSONValue* value );
+            __declspec(dllexport) bool removeValue( Text field );
+            __declspec(dllexport) bool hasValue( Text field );
+            __declspec(dllexport) JSONValue* getValue( Text field );
+            __declspec(dllexport) Iterator< Text > getFields();
+            __declspec(dllexport) Iterator< JSONValue* > getValues();
+            __declspec(dllexport) int getFieldCount() const;
+            __declspec(dllexport) bool isValueOfType( Text field, JSONType type ) const;
+
+            __declspec(dllexport) Text toString() const override;
         };
 
-        __declspec( dllexport ) JSONValue *loadJSONFromFile( Text path );
+        __declspec(dllexport) JSONValue* loadJSONFromFile( Text path );
 
         namespace Parser
         {
-            __declspec( dllexport ) int findObjectEndInArray( const char *str );
-            __declspec( dllexport ) Text removeWhitespace( const char *str );
-            __declspec( dllexport ) JSONValue *getValue( const char *str );
-            __declspec( dllexport ) int findFieldEndInObject( const char *str );
-            __declspec( dllexport ) int findValueEndInObject( const char *str );
+            __declspec(dllexport) int findObjectEndInArray( const char* str );
+            __declspec(dllexport) Text removeWhitespace( const char* str );
+            __declspec(dllexport) JSONValue* getValue( const char* str );
+            __declspec(dllexport) int findFieldEndInObject( const char* str );
+            __declspec(dllexport) int findValueEndInObject( const char* str );
         };
     }
 }