Loki-Astari / ThorsMongo

C++ MongoDB API and BSON/JSON Serialization library
GNU General Public License v3.0
316 stars 71 forks source link

add more serialization type #25

Closed timercrack closed 8 years ago

timercrack commented 8 years ago

I have a struct like this:

struct Profile { char name[20]; char type; int age; };

when compile, it says:

error: static assertion failed: Trying to serialize an object that does not have a ThorsAnvil::Serialize::Trait<> defined.Look at macro ThorsAnvil_MakeTrait() for more information.

Could you please add support for char or char[] ?

Loki-Astari commented 8 years ago

Can you provide a minimal compilable example so I can reproduce.

timercrack commented 8 years ago

here is my example3.cpp

#include "ThorSerialize/JsonThor.h"

struct TEST
{
    char name[20];  // the length of the array vary in other struct
    char sex;
    int age;
};

ThorsAnvil_MakeTrait(TEST, name, sex, age);

int main()
{
    TEST test {"timercrack", 'm', 30};
    std::cout << ThorsAnvil::Serialize::jsonExport(test) << std::endl;
}

use this command to compile:

g++ -std=c++14 -o example3 example3.cpp -lThorSerialize17

and the console output is:

# jeffchen @ ubuntu in /media/psf/Home/Documents/codes/ThorsSerializer/doc on git:master x [9:34:13]
$ g++ -std=c++14 -o example3 example3.cpp -lThorSerialize17
In file included from /usr/local/include/ThorSerialize/Serialize.h:297:0,
                 from /usr/local/include/ThorSerialize/JsonParser.h:21,
                 from /usr/local/include/ThorSerialize/JsonThor.h:15,
                 from example3.cpp:1:
/usr/local/include/ThorSerialize/Serialize.tpp: In instantiation of ‘ThorsAnvil::Serialize::SerializeMember<T, M, (ThorsAnvil::Serialize::TraitType)2>::SerializeMember(ThorsAnvil::Serialize::PrinterInterface&, const T&, const std::pair<const char*, M T::*>&) [with T = TEST; M = char [20]]’:
/usr/local/include/ThorSerialize/Serialize.tpp:402:61:   required from ‘ThorsAnvil::Serialize::SerializeMember<T, M> ThorsAnvil::Serialize::make_SerializeMember(ThorsAnvil::Serialize::PrinterInterface&, const T&, const std::pair<const char*, M T::*>&) [with T = TEST; M = char [20]; typename std::remove_cv<M>::type = char [20]]’
/usr/local/include/ThorSerialize/Serialize.tpp:410:42:   required from ‘void ThorsAnvil::Serialize::Serializer::printEachMember(const T&, const Members&, std::index_sequence<Seq ...>&) [with T = TEST; Members = std::tuple<std::pair<const char*, char (TEST::*)[20]>, std::pair<const char*, char TEST::*>, std::pair<const char*, int TEST::*> >; long unsigned int ...Seq = {0ul, 1ul, 2ul}; std::index_sequence<Seq ...> = std::integer_sequence<long unsigned int, 0ul, 1ul, 2ul>]’
/usr/local/include/ThorSerialize/Serialize.tpp:417:5:   required from ‘void ThorsAnvil::Serialize::Serializer::printMembers(const T&, const std::tuple<_Elements ...>&) [with T = TEST; Members = {std::pair<const char*, char (TEST::*)[20]>, std::pair<const char*, char TEST::*>, std::pair<const char*, int TEST::*>}]’
/usr/local/include/ThorSerialize/Serialize.tpp:461:5:   required from ‘void ThorsAnvil::Serialize::Serializer::printObjectMembers(const T&) [with T = TEST]’
/usr/local/include/ThorSerialize/Serialize.tpp:313:13:   required from ‘void ThorsAnvil::Serialize::SerializerForBlock<traitType, T>::printMembers() [with ThorsAnvil::Serialize::TraitType traitType = (ThorsAnvil::Serialize::TraitType)3; T = TEST]’
/usr/local/include/ThorSerialize/Serialize.tpp:430:5:   required from ‘void ThorsAnvil::Serialize::Serializer::print(const T&) [with T = TEST]’
/usr/local/include/ThorSerialize/Exporter.h:33:13:   required from ‘std::ostream& ThorsAnvil::Serialize::operator<<(std::ostream&, const ThorsAnvil::Serialize::Exporter<ThorsAnvil::Serialize::Json, TEST>&)’
example3.cpp:15:56:   required from here
/usr/local/include/ThorSerialize/Serialize.tpp:395:13: error: use of deleted function ‘void ThorsAnvil::Serialize::PrinterInterface::addValue(const void*)’
             printer.addValue(object.*(memberInfo.second));
             ^
In file included from /usr/local/include/ThorSerialize/JsonParser.h:21:0,
                 from /usr/local/include/ThorSerialize/JsonThor.h:15,
                 from example3.cpp:1:
/usr/local/include/ThorSerialize/Serialize.h:123:14: note: declared here
         void addValue(void const*)  = delete;

If I change name and sex's type to std::string, all things is OK. So I think the problem is it doesn't support there types.

Loki-Astari commented 8 years ago

I looked back at my notes.

There are issues with serializing arrays as there is no automated way of finding the size so serialization is tricky and de-serialization is just dangerous. As a result I made a deliberate decision not provide this for arrays.

Unless you are building for very low level services the recommendation is not to use arrays anyway. You should be using std::array/std::vector or std::string or some high level construct that is safer than your basic array.

I would also note this:

g++ -std=c++14 -o example3 example3.cpp -lThorSerialize17
            ^^                                         ^^

You are building your source as C++14 but the library has been built as C++17. It may work but I doubt the compiler gives you any guarantees about that.

I don't plan on writing the extension to handle arrays. But if you decide this is required for your project and want to write the extension required I am more than happy to review a pull request and integrate it into the project.

Loki-Astari commented 8 years ago

Not going to fix. But if somebody provides a pull request I will look at it to make sure it is logical.