Files
ArduinoJson/JsonGenerator/JsonValue.h

135 lines
3.3 KiB
C
Raw Permalink Normal View History

2014-06-27 13:42:26 +02:00
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "EscapedString.h"
2014-07-01 13:44:36 +02:00
#include "Printable.h"
2014-06-27 13:42:26 +02:00
#include "StringBuilder.h"
2014-07-03 13:54:27 +02:00
namespace ArduinoJson
{
namespace Generator
2014-06-27 13:42:26 +02:00
{
class JsonValue
2014-07-03 13:54:27 +02:00
{
public:
2014-07-22 21:02:16 +02:00
2014-07-31 19:48:51 +02:00
void operator=(bool value)
2014-07-03 13:54:27 +02:00
{
printToImpl = &printBoolTo;
2014-07-03 13:54:27 +02:00
content.asBool = value;
}
2014-06-27 13:42:26 +02:00
2014-07-31 19:48:51 +02:00
void operator=(long value)
2014-07-03 13:54:27 +02:00
{
printToImpl = &printLongTo;
2014-07-03 13:54:27 +02:00
content.asLong = value;
}
2014-07-31 19:48:51 +02:00
void operator=(int value)
2014-07-03 13:54:27 +02:00
{
printToImpl = &printLongTo;
2014-07-03 13:54:27 +02:00
content.asLong = value;
}
void operator=(const Printable& value)
2014-07-03 13:54:27 +02:00
{
printToImpl = &printPrintableTo;
2014-07-03 13:54:27 +02:00
content.asPrintable = &value;
}
2014-07-31 19:48:51 +02:00
void operator=(const char* value)
2014-07-03 13:54:27 +02:00
{
printToImpl = &printStringTo;
2014-08-01 15:22:30 +02:00
content.asString = value;
2014-07-03 13:54:27 +02:00
}
2014-07-31 19:48:51 +02:00
void operator=(double value)
2014-07-07 19:50:19 +02:00
{
set<2>(value);
}
2014-07-22 21:02:16 +02:00
template <int DIGITS>
void set(double value)
{
2014-07-31 19:48:51 +02:00
printToImpl = &printDoubleTo < DIGITS > ;
content.asDouble = value;
}
2014-08-01 14:54:34 +02:00
operator bool()
{
return content.asBool;
}
2014-08-01 14:35:54 +02:00
operator const char*()
{
2014-08-01 15:23:14 +02:00
return content.asString;
2014-08-01 14:35:54 +02:00
}
2014-08-01 14:56:46 +02:00
operator double()
{
return content.asDouble;
}
2014-08-01 14:58:16 +02:00
operator float()
{
return (float)content.asDouble;
}
2014-08-01 14:52:15 +02:00
operator int()
{
return content.asLong;
}
2014-08-01 14:53:05 +02:00
operator long()
{
return content.asLong;
}
operator const Printable&()
{
return *content.asPrintable;
}
size_t printTo(Print& p) const
2014-07-03 13:54:27 +02:00
{
// handmade polymorphism
return printToImpl(content, p);
2014-07-03 13:54:27 +02:00
}
void reset()
{
content.asDouble = 0;
printToImpl = printStringTo;
}
2014-07-03 13:54:27 +02:00
private:
union Content
{
bool asBool;
double asDouble;
long asLong;
const Printable* asPrintable;
const char* asString;
2014-07-03 13:54:27 +02:00
};
Content content;
2014-07-31 19:48:51 +02:00
size_t(*printToImpl)(const Content&, Print&);
static size_t printBoolTo(const Content&, Print&);
static size_t printLongTo(const Content&, Print&);
static size_t printPrintableTo(const Content&, Print&);
static size_t printStringTo(const Content&, Print&);
2014-07-03 13:54:27 +02:00
2014-07-22 21:02:16 +02:00
template <int DIGITS>
static size_t printDoubleTo(const Content& c, Print& p)
{
return p.print(c.asDouble, DIGITS);
}
2014-07-03 13:54:27 +02:00
};
}
2014-07-03 13:54:27 +02:00
}