Skip to content

weird problem lates version #2212

@Philwzen

Description

@Philwzen

Description
a function works fine when in the script but fails if in an include file.

Troubleshooter's report

  1. The program uses ArduinoJson 7
  2. The issue happens at run time
  3. The issue concerns serialization
  4. Program crashes
  5. Program doesn't use PROGMEM
  6. Program doesn't insert string pointers into the JsonDocument
  7. Output type is String

Environment

  • Microcontroller: esp32 TinyPico
  • Core/Framework: name=ESP32 Arduino version=3.3.3
  • IDE: Arduino ide 1.8.19

Reproduction code

#include <ArduinoJson.h>

//#define UseInclude

#ifdef UseInclude
 #include "Zenjson.h"     // fails !!
#else // exact copy of .h !! works
// from here straight copy paste into Zenjson.h
JsonDocument AddParam(JsonDocument& jsonhandle,const String& Var,const bool& Val)
{
  jsonhandle = AddParam(jsonhandle,Var,String(Val));
  return jsonhandle;
}

JsonDocument AddParam(JsonDocument& jsonhandle,const String& Var,const float& Val)
{
  jsonhandle = AddParam(jsonhandle,Var,String(Val));
  return jsonhandle;
}

JsonDocument AddParam(JsonDocument& jsonhandle,const String& Var,const int& Val)
{
  jsonhandle = AddParam(jsonhandle,Var,String(Val));
  return jsonhandle;
}

JsonDocument AddParam(JsonDocument& jsonhandle,const String& Var,const String& Val)
{
  Serial.println("here 1");
  jsonhandle[Var] = Val;
  Serial.println("here 2");
  return jsonhandle;
}

void DisplayParams(JsonDocument& jsonhandle)
{
  for (JsonPair kv : jsonhandle.as<JsonObject>()) 
  {
     Serial.print(kv.key().c_str());
     Serial.println(kv.value().as<String>());
  }
}
//end copy paste
#endif

String BuildOrec(JsonDocument JLP)
{
  String Ts = "12:12:2025210000"; //GetTimeStamp();
  JLP = AddParam(JLP,"DateStamp",Ts.substring(0,10));
  JLP = AddParam(JLP,"TimeStamp",Ts.substring(11));
  String Orec;
  serializeJson(JLP, Orec);
  Orec =  "\"**" +  Orec + "\"";
  Serial.println(Orec);
  return Orec;
}

void LogTest()
{
  String action = "testing";
  String User   = "Phil";
  bool   result = true;
  float  num    = 12.435;
  int    phone  = 1234567;
  String scratch;
  
  JsonDocument JLP;
  JLP = AddParam(JLP,"Number",phone); // it fails on first call if using include
  JLP = AddParam(JLP,"Amount",num);
  JLP = AddParam(JLP,"Action",action);
  JLP = AddParam(JLP,"Result",result);
  JLP = AddParam(JLP,"User",User);
  
  DisplayParams(JLP);
  scratch = BuildOrec(JLP);
  JLP.clear();
}


void setup() {
  Serial.begin(115200);
  LogTest();
}

void loop() {

}

this is the contents of Zenjson

JsonDocument AddParam(JsonDocument& jsonhandle,const String& Var,const bool& Val)
{
jsonhandle = AddParam(jsonhandle,Var,String(Val));
return jsonhandle;
}

JsonDocument AddParam(JsonDocument& jsonhandle,const String& Var,const float& Val)
{
jsonhandle = AddParam(jsonhandle,Var,String(Val));
return jsonhandle;
}

JsonDocument AddParam(JsonDocument& jsonhandle,const String& Var,const int& Val)
{
jsonhandle = AddParam(jsonhandle,Var,String(Val));
return jsonhandle;
}

JsonDocument AddParam(JsonDocument& jsonhandle,const String& Var,const String& Val)
{
Serial.println("here 1");
jsonhandle[Var] = Val;
Serial.println("here 2");
return jsonhandle;
}

void DisplayParams(JsonDocument& jsonhandle)
{
for (JsonPair kv : jsonhandle.as())
{
Serial.print(kv.key().c_str());
Serial.println(kv.value().as());
}
}

Remarks
weird i always thought a .h was just a straight dropin of its code same as cut paste but this is different if code is in include it causes core 1 panic. i have no idea why if its pasted in in stead it works fine.
i cant post the include but its just the code in #else

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions