Пример #1
0
        static void Helper(CStateInfo& state, void* next, const string &nextName)
        {
            state.s += RepeatString("\t", state.depth);
            state.s += "{\n";
            state.depth++;

            TypeInfo *typeInfo = TypeInfo::GetTypeInfo(nextName);
            while (typeInfo)
            {
                map<string, PropertyInfo*> &props = typeInfo->Properties();
                for (map<string, PropertyInfo*>::iterator i = props.begin(); i != props.end(); ++i)
                {
                    map<string, PropertyInfo*>::iterator nextIter = ++i;
                    i--;

                    if (i->second->Integral())
                    {
                        if ( i->second->IsPointer() )
                        {
                            printf( "we are here ");
                        }
                        void *value = i->second->GetValue(next);
                        std::string stringTypeName = i->second->TypeName();
                        TypeInfo* _typeInfo = TypeInfo::GetTypeInfo(stringTypeName);
                        std::string stringValue = _typeInfo->GetString(value);

                        state.s += RepeatString("\t", state.depth) +
                                   "\"" +
                                   i->second->Name() +
                                   "\" : \"" +
                                   stringValue.c_str() +
                                   "\"" +
                                   RepeatString(",", typeInfo->BaseInfo() != 0 || nextIter != props.end()) +
                                   "\n";
                    }
                    else if (i->second->IsArray())
                    {
                        state.s += RepeatString("\t", state.depth) +
                                   "\"" +
                                   i->second->Name() +
                                   "\" : \n" +
                                   RepeatString("\t", state.depth) +
                                   "[\n";
                        state.depth++;
                        for (int j = 0; j < i->second->GetArraySize(next); j++)
                        {
                            void *value = i->second->GetValue(next, j);
                            char floatStr[256];
                            sprintf(floatStr, "%f", *(static_cast<float*>(value)));

                            state.s += RepeatString("\t", state.depth) +
                                       "\"" +
                                       floatStr +
                                       "\"" +
                                       RepeatString(",", j != i->second->GetArraySize(next) - 1 ) +
                                       "\n";
                            delete value;
                        }
                        state.depth--;
                        state.s += RepeatString("\t", state.depth) +
                                   "]" +
                                   RepeatString(",", nextIter != props.end() || typeInfo->BaseInfo()) +
                                   "\n";
                    }
                    else
                    {
                        state.s += RepeatString("\t", state.depth) +
                                   "\"" +
                                   i->second->Name() +
                                   "\" : \n";
                        bool alreadySerialized = false;
                        bool nullPtr = false;
                        if ( i->second->IsPointer() )
                        {
                            if (i->second->GetValue(next) == 0)
                                nullPtr = true;
                            if (nullPtr)
                            {
                                state.s += "0";
                            }
                            else
                            {
                                alreadySerialized = state.addresTable.count( i->second->GetValue(next) ) == 1;
                                if (alreadySerialized)
                                {
                                    state.s += "\"@ptr" +
                                               To<int>::String( state.addresTable[ i->second->GetValue(next) ] ) +
                                               "\",\n";
                                }
                                else
                                {
                                    state.ptrCount++;
                                    state.addresTable[ i->second->GetValue(next) ] = state.ptrCount;
                                    state.s += RepeatString("\t", state.depth) +
                                               "{\n" +
                                               RepeatString("\t", state.depth + 1) +
                                               "\"@ptr\" : " +
                                               To<int>::String(state.ptrCount) +
                                               ",\n" +
                                               RepeatString("\t", state.depth + 1) +
                                               "\"@value\" :\n";
                                    state.depth++;
                                }
                            }
                        }
                        if (!alreadySerialized && !nullPtr)
                        {
                            void *value = i->second->GetValue(next);
                            Helper(state, value, i->second->TypeName());
                            if ( i->second->IsPointer() )
                            {
                                state.depth--;
                                state.s += RepeatString("\t", state.depth) +
                                           "}\n";
                            }
                            else
                                delete value;
                        }
                    }
                }
                typeInfo = typeInfo->BaseInfo();
            }
            state.depth--;
            state.s += RepeatString( "\t", state.depth );
            if (state.depth == 0)
                state.s += "}\n";
            else
                state.s += "},\n";
        }