arduino-libraries / Arduino_JSON

Official JSON Library for Arduino
GNU Lesser General Public License v2.1
151 stars 60 forks source link

Add support for parsing date #43

Closed Zhu-jiatong closed 1 year ago

Zhu-jiatong commented 1 year ago

An example of such a scenario is when users need to synchronise time between their device and the web server. The JavaScript code below is used to send the client's date & time to the server.

const d = new Date();
let text = d.toJSON();
// then post text to the server.

The JSON string posted would be formatted like this: 2022-11-08T02:49:50.267Z

Currently, it appears that this library is incapable of parsing this JSON format. Thus, I propose the addition of this functionality.

Thank you.

per1234 commented 1 year ago

Hi @Zhu-jiatong . Thanks for your suggestion.

Parsing raw date strings into JSON data works just find for me:

#include <Arduino_JSON.h>
void setup() {
  Serial.begin(9600);
  while (!Serial);
  delay(1000);

  JSONVar myString = "2022-11-08T02:49:50.267Z";

  Serial.print("type: ");
  Serial.println(JSON.typeof(myString));

  Serial.print("myString: ");
  Serial.println(myString);

  Serial.print("(const char*)myString: ");
  Serial.println((const char*) myString);
}
void loop() {}

As for any enhanced processing of dates, the scope of this library is parsing and generation of JSON data. A library dedicated to handling dates should be used for any date-specific requirements.