I'm trying to create a function that enters a key/value into a JSON file.
But, since value can be an int
or char
, I don't want to create 2 functions for each type.
Is there a way ?
void setValue(char *key, char* value){
StaticJsonDocument<512> tempJDOC;
tempJDOC[key]=value;
}
EDIT1
#ifndef myJSON_h
#define myJSON_h
#include "Arduino.h"
#include <ArduinoJson.h>
#include "FS.h"
class myJSON
{
private:
bool _useSerial=false;
char _filename[30];
public:
char *ver="myJSON_v1.1";
myJSON(char *filename, bool useserial=false);
bool file_exists();
bool file_remove();
bool format ();
bool FS_ok();
void saveJSON2file(JsonDocument& _doc);
void readJSON_file(JsonDocument& _doc);
void printJSON(JsonDocument& _doc);
void PrettyprintJSON(JsonDocument& _doc);
const char *getValue (char *key);
template <class T>
void setValue(const char *key, T value);
//void setValue(const char *key, char *value);
};
#endif
EDIT2
#ifndef myJSON_h
#define myJSON_h
#include "Arduino.h"
#include <ArduinoJson.h>
#include "FS.h"
#define LOG_LENGTH 4
#define DOC_SIZE 512
template <class T> // . <---- line added
class myJSON
{
private:
bool _useSerial=false;
char _filename[30];
public:
char *ver="myJSON_v1.1";
myJSON(char *filename, bool useserial=false);
bool file_exists();
bool file_remove();
bool format ();
bool FS_ok();
void saveJSON2file(JsonDocument& _doc);
void readJSON_file(JsonDocument& _doc);
void printJSON(JsonDocument& _doc);
void PrettyprintJSON(JsonDocument& _doc);
const char *getValue (const char *key);
void removeValue(const char *key);
// void setValue(const char *key, char *value); <--- comment out
void updateArray(char* array_key, int val);
void printFile();
void setValue(const char *key, T value); <---- Added
};
#endif
and in .cpp file :
void myJSON::setValue(const char *key, T value){
StaticJsonDocument<512> tempJDOC;
myJSON::readJSON_file(tempJDOC);
tempJDOC[key]=value;
myJSON::saveJSON2file(tempJDOC);
myJSON::PrettyprintJSON(tempJDOC);
}
2 Answers 2
In C++ you can create a template function. (The ArduinoJson []
operator is a template too.)
#include <ArduinoJson.h>
StaticJsonDocument<512> tempJDOC;
template <typename T>
void setValue(const char *key, T value){
tempJDOC[key] = value;
}
void setup() {
Serial.begin(115200);
setValue("a", 5);
setValue("b", "xyz");
serializeJson(tempJDOC, Serial);
}
void loop() {
}
the compiler will create function based on this template for every different type you use as second parameter in your sketch
-
When trying to take such function and put it inside a library ( using
template
as noted here ), i get an error. how can it be done?guyd– guyd2019年04月29日 20:22:49 +00:00Commented Apr 29, 2019 at 20:22 -
by "put it inside a library", you mean using it as class's method?2019年04月30日 04:54:15 +00:00Commented Apr 30, 2019 at 4:54
-
a method that uses template inside a classguyd– guyd2019年04月30日 05:42:02 +00:00Commented Apr 30, 2019 at 5:42
-
add the code of the class to the Question. is it a class with separate .h and .cpp files? do you have the template<> part of function declaration in .h too?2019年04月30日 07:23:39 +00:00Commented Apr 30, 2019 at 7:23
-
The short answer is "no, it's not possible". You will have to use two different functions. These two functions can share the same name, using function overloading. You can even have the compiler write these functions for you, based on a template you provide, as shown in Juraj's answer. These language features help provide the illusion of there being a single function. But it's just an illusion, albeit a useful one that helps with program readability. At the fundamental level, you still have two different functions.
-
Edgar - thank you for explaining and usefull link. But I do not understand how Juraj's answer ( using template ) creates/ duplicates the function for other typesguyd– guyd2019年04月27日 20:22:37 +00:00Commented Apr 27, 2019 at 20:22
-
1@Guy.D: Do a search for "C++ templates". In short, every time you call
setValue()
with a new value type, the compiler creates a new version of thesetValue()
function suitable for that specific type.Edgar Bonet– Edgar Bonet2019年04月27日 20:26:55 +00:00Commented Apr 27, 2019 at 20:26 -
1I have in my answer: "the compiler will create function based on this template for every different type you use as second parameter in your sketch"2019年04月30日 13:42:03 +00:00Commented Apr 30, 2019 at 13:42
-
@EdgarBonet I use you link to try to implement this function inside a lib I make ( called
myJSON
, as you can see in edit 1 and edit 2 ). I'll appreciate your help once moreguyd– guyd2019年05月01日 06:00:52 +00:00Commented May 1, 2019 at 6:00
char
parameter. The code seems to suggest that you are actually talking aboutchar *
parameter. So, what is it?char
orchar *
? These are two very different things. You have to edit your question and clarify that.template
- so I don't need to useint
norchar
int
orchar
you wouldn't need to do anything at all. You don't need any "templates". Just oneint
version of the function by itself would cover all cases, includingchar
, since in C and C++char
is just a small integer type. So, again, why are you asking this question? What is the problem withchar
that triggered it? Again, the code you provided implies that you needchar *
(or, better,const char *
). Notchar
, butconst char *
. You need to reflect it in your question, since currently neither the question not the accepted answer make sense.int
orconst char *
, albeit you are doing it implicitly. The compiler does it for you. But again, it is a matter ofint
andconst char *
, not a matter ofint
andchar
as you stated in your question.char
is completely irrelevant here.