patterncppMinor
Serialization: Step 1 Json Parser
Viewed 0 times
jsonparserserializationstep
Problem
As nobody has provided input, I have updated the question. (The next one is coming soon)
Coding to this interface:
The Json Implementation is:
JsonParser.h
```
#ifndef THORS_ANVIL_SERIALIZATION_JSON_PARSER_H
#define THORS_ANVIL_SERIALIZATION_JSON_PARSER_H
#include "Serialize.h"
#include "JsonLexer.h"
#include
#include
#include
namespace ThorsAnvil
{
namespace Serialization
{
class JsonParser: public ParserInterface
{
enum State {Error, Init, OpenM, Key, Colon, ValueM
Coding to this interface:
namespace ThorsAnvil
{
namespace Serialization
{
class ParserInterface
{
public:
enum class ParserToken {Error, DocStart, DocEnd, MapStart, MapEnd, ArrayStart, ArrayEnd, Key, Value};
std::istream& input;
ParserToken pushBack;
ParserInterface(std::istream& input)
: input(input)
, pushBack(ParserToken::Error)
{}
virtual ~ParserInterface() {}
ParserToken getToken();
void pushBackToken(ParserToken token);
virtual ParserToken getNextToken() = 0;
virtual std::string getKey() = 0;
virtual void getValue(short int&) = 0;
virtual void getValue(int&) = 0;
virtual void getValue(long int&) = 0;
virtual void getValue(long long int&) = 0;
virtual void getValue(unsigned short int&) = 0;
virtual void getValue(unsigned int&) = 0;
virtual void getValue(unsigned long int&) = 0;
virtual void getValue(unsigned long long int&)= 0;
virtual void getValue(float&) = 0;
virtual void getValue(double&) = 0;
virtual void getValue(long double&) = 0;
virtual void getValue(bool&) = 0;
virtual void getValue(std::string&) = 0;
};
}
}The Json Implementation is:
JsonParser.h
```
#ifndef THORS_ANVIL_SERIALIZATION_JSON_PARSER_H
#define THORS_ANVIL_SERIALIZATION_JSON_PARSER_H
#include "Serialize.h"
#include "JsonLexer.h"
#include
#include
#include
namespace ThorsAnvil
{
namespace Serialization
{
class JsonParser: public ParserInterface
{
enum State {Error, Init, OpenM, Key, Colon, ValueM
Solution
As an attempt to kill cure (too bad they are still far from being homophones) zombies, let me try write something here. Here's some suggestions:
-
The indentation looks absurd to me:
I would typically do
If you really want to get rid of the indentation, you can flush the namespace declarations to the left altogether.
-
There has to be a better way of writing the 17
-
Are you sure these belong in a general interface? I don't think so.
-
Since
-
Is this necessary? At least the comment should be removed. Personally, I would remove the default branch altogether, but that's pretty much a matter of state.
-
The tables used in the implementation should probably be
-
The indentation looks absurd to me:
namespace ThorsAnvil
{
namespace Serialization
{
class ParserInterface
{
// ...
};
}
}I would typically do
namespace ThorsAnvil::Serialization {
class ParserInterface {
// ...
};
}If you really want to get rid of the indentation, you can flush the namespace declarations to the left altogether.
-
There has to be a better way of writing the 17
getValue functions.-
Are you sure these belong in a general interface? I don't think so.
public:
std::istream& input;
ParserToken pushBack;-
Since
scan is a template, you should implement it in the header.-
Is this necessary? At least the comment should be removed. Personally, I would remove the default branch altogether, but that's pretty much a matter of state.
// Anything else just break.
default:
break;-
The tables used in the implementation should probably be
const.Code Snippets
namespace ThorsAnvil
{
namespace Serialization
{
class ParserInterface
{
// ...
};
}
}namespace ThorsAnvil::Serialization {
class ParserInterface {
// ...
};
}public:
std::istream& input;
ParserToken pushBack;// Anything else just break.
default:
break;Context
StackExchange Code Review Q#79279, answer score: 2
Revisions (0)
No revisions yet.