Skip to content

serializeJson to SPIFFS File Will Cause Data Lost. #8655

Closed
@toniz

Description

----------------------------- Delete below -----------------------------

  1. serializeJson to SPIFFS File When Json Length Is 263, 271, 279 etc..
  2. Call SPIFFS.exists("other_file.txt")
  3. The last three byte will change to : ff ff ff

ps: serializeJson to String, then to file is ok.

Example code:

#include <FS.h>
#include <ArduinoJson.h>

void setup() {
    Serial.begin(9600);
    
    if(!SPIFFS.begin()){
        Serial.println("SPIFFS Failed to Start.");
    }
    
    DynamicJsonDocument doc(2048);
    doc["wifi"] = "eyJ1c2VyXwMjIiLCJ0b2tlbiI6IjZhZTI2MjIxY2E1MGM3MWQ1ODMzNjdkM2ZlNTgyNGRmM2I4MTcwZGEwZmU0NDQ5M2VjOWY0OWVhNjZjMTllMTk4MmEwOTgwNDAzNDBiMmMxNWU3YjAyZjBlZDRjNTM2NzgzNzlkNDgxYjcxMDk4ZTRkMzlmZGIxNiJ9123123124asdfasdfasdfaasdfasdfdsf131234124asdfsadffadf12345561";

    //serializeJson to String, then to file is ok.
    //String text;
    //serializeJson(doc, text);
    File file = SPIFFS.open("wifi.txt", "w");
    //file.print(text);
    serializeJson(doc, file); //bug
    file.close();

    LoadWifiConfig();
    SPIFFS.exists("2.txt"); //call other file name 
    LoadWifiConfig();

}

void loop() {
}

void LoadWifiConfig() {
    String encText;
    File file = SPIFFS.open("wifi.txt", "r");
    while (file.available()) {
        char abc = (char)file.read();
        if (abc < 0x10) {
            Serial.print("0");
        }
        Serial.print(String(abc, HEX));
        encText += abc;
    }
    file.close();
    Serial.println("");
    Serial.println("From Text:");
    Serial.println(encText);
    Serial.println(encText.length());
    return;
}

Debug Messages

10:02:38.739 -> �⸮�@⸮⸮⸮�⸮b⸮⸮⸮SPIFFSImpl: allocating 512+240+1400=2152 bytes
10:02:38.851 -> SPIFFSImpl: mounting fs @200000, size=1fa000, block=2000, page=100
10:02:39.001 -> SPIFFSImpl: mount rc=0
10:02:39.001 -> 
10:02:39.038 -> SPIFFS_close: fd=1
10:02:39.038 -> SPIFFS_close: fd=1
10:02:39.075 -> 7b2277696669223a2265794a316332567958774d6a49694c434a306232746c62694936496a5a685a5449324d6a4978593245314d474d334d5751314f444d7a4e6a646b4d325a6c4e5467794e47526d4d3249344d5463775a4745775a6d55304e4451354d32566a4f5759304f5756684e6a5a6a4d546c6c4d546b344d6d45774f5467774e44417a4e4442694d6d4d784e[String] Reallocating large String(143 -> 144 bytes) '{"wifi":"e ... AzNDBiMmMx'
10:02:39.447 -> 575533596a41795a6a426c5a44526a4e[String] Reallocating large String(159 -> 160 bytes) '{"wifi":"e ... AyZjBlZDRj'
10:02:39.559 -> 544d324e7a677a4e7a6c6b4e44677859[String] Reallocating large String(175 -> 176 bytes) '{"wifi":"e ... gzNzlkNDgx'
10:02:39.671 -> 6a63784d446b345a54526b4d7a6c6d5a[String] Reallocating large String(191 -> 192 bytes) '{"wifi":"e ... k4ZTRkMzlm'
10:02:39.821 -> 4749784e694a39313233313233313234[String] Reallocating large String(207 -> 208 bytes) '{"wifi":"e ... J912312312'
10:02:39.934 -> 61736466617364666173646661617364[String] Reallocating large String(223 -> 224 bytes) '{"wifi":"e ... sdfasdfaas'
10:02:40.046 -> 66617364666473663133313233343132[String] Reallocating large String(239 -> 240 bytes) '{"wifi":"e ... dsf1312341'
10:02:40.159 -> 34617364667361646666616466313233[String] Reallocating large String(255 -> 256 bytes) '{"wifi":"e ... sadffadf12'
10:02:40.268 -> 3435353631227dSPIFFS_close: fd=1
10:02:40.306 -> SPIFFS_close: fd=1
10:02:40.343 -> 
10:02:40.343 -> From Text:
10:02:40.343 -> {"wifi":"eyJ1c2VyXwMjIiLCJ0b2tlbiI6IjZhZTI2MjIxY2E1MGM3MWQ1ODMzNjdkM2ZlNTgyNGRmM2I4MTcwZGEwZmU0NDQ5M2VjOWY0OWVhNjZjMTllMTk4MmEwOTgwNDAzNDBiMmMxNWU3YjAyZjBlZDRjNTM2NzgzNzlkNDgxYjcxMDk4ZTRkMzlmZGIxNiJ9123123124asdfasdfasdfaasdfasdfdsf131234124asdfsadffadf12345561"}
10:02:40.634 -> 263
10:02:40.634 -> 7b2277696669223a2265794a316332567958774d6a49694c434a306232746c62694936496a5a685a5449324d6a4978593245314d474d334d5751314f444d7a4e6a646b4d325a6c4e5467794e47526d4d3249344d5463775a4745775a6d55304e4451354d32566a4f5759304f5756684e6a5a6a4d546c6c4d546b344d6d45774f5467774e44417a4e4442694d6d4d784e[String] Reallocating large String(143 -> 144 bytes) '{"wifi":"e ... AzNDBiMmMx'
10:02:41.006 -> 575533596a41795a6a426c5a44526a4e[String] Reallocating large String(159 -> 160 bytes) '{"wifi":"e ... AyZjBlZDRj'
10:02:41.119 -> 544d324e7a677a4e7a6c6b4e44677859[String] Reallocating large String(175 -> 176 bytes) '{"wifi":"e ... gzNzlkNDgx'
10:02:41.230 -> 6a63784d446b345a54526b4d7a6c6d5a[String] Reallocating large String(191 -> 192 bytes) '{"wifi":"e ... k4ZTRkMzlm'
10:02:41.379 -> 4749784e694a39313233313233313234[String] Reallocating large String(207 -> 208 bytes) '{"wifi":"e ... J912312312'
10:02:41.492 -> 61736466617364666173646661617364[String] Reallocating large String(223 -> 224 bytes) '{"wifi":"e ... sdfasdfaas'
10:02:41.602 -> 66617364666473663133313233343132[String] Reallocating large String(239 -> 240 bytes) '{"wifi":"e ... dsf1312341'
10:02:41.715 -> 34617364667361646666616466313233[String] Reallocating large String(255 -> 256 bytes) '{"wifi":"e ... sadffadf12'
10:02:41.826 -> 34353536ffffffSPIFFS_close: fd=1
10:02:41.864 -> SPIFFS_close: fd=1
10:02:41.902 -> 
10:02:41.902 -> From Text:
10:02:41.902 -> {"wifi":"eyJ1c2VyXwMjIiLCJ0b2tlbiI6IjZhZTI2MjIxY2E1MGM3MWQ1ODMzNjdkM2ZlNTgyNGRmM2I4MTcwZGEwZmU0NDQ5M2VjOWY0OWVhNjZjMTllMTk4MmEwOTgwNDAzNDBiMmMxNWU3YjAyZjBlZDRjNTM2NzgzNzlkNDgxYjcxMDk4ZTRkMzlmZGIxNiJ9123123124asdfasdfasdfaasdfasdfdsf131234124asdfsadffadf1234556⸮⸮⸮
10:02:42.194 -> 263

In this code,
1"} => ff ff ff

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions