Skip to content

Instantly share code, notes, and snippets.

@arcao
Last active October 19, 2023 19:44
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arcao/3252bb6e5e52493f03726ec32e61395c to your computer and use it in GitHub Desktop.
Save arcao/3252bb6e5e52493f03726ec32e61395c to your computer and use it in GitHub Desktop.
StringStream class / Library for Arduino

StringStream class / Library for Arduino

This piece of code is usable when you need Stream object which has to store/load data to/from Arduino String object.

Source (StringStream.h)

#ifndef _STRING_STREAM_H_
#define _STRING_STREAM_H_

#include <Stream.h>

class StringStream : public Stream
{
public:
    StringStream(String *s) : string(s), position(0) { }

    // Stream methods
    virtual int available() { return string->length() - position; }
    virtual int read() { return position < string->length() ? (*string)[position++] : -1; }
    virtual int peek() { return position < string->length() ? (*string)[position] : -1; }
    virtual void flush() { };
    // Print methods
    virtual size_t write(uint8_t c) { (*string) += (char)c; return 1;};

private:
    String *string;
    unsigned int length;
    unsigned int position;
};

#endif // _STRING_STREAM_H_

Usage

Example with ArduinoJsonWriter library...

#include <JsonWriter.h>
#include "StringStream.h"

void setup() {
  Serial.begin(9600);

  String output;
  StringStream stream(&output);
  JsonWriter streamJsonWriter(&stream);

  streamJsonWriter
  .beginArray()
                .beginObject()
                    .property("name", "alpha")
                .endObject()
                .beginObject()
                    .property("name", "beta")
                    .property("anotherProperty", 12.67)
                .endObject()
                .beginObject()
                    .property("name", "gamma")
                .endObject()
    .endArray();
    
    Serial.println(output);
}

void loop() {}

License

BSD Zero Clause License

Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@jarija
Copy link

jarija commented May 24, 2018

Hi!,

If I try to run your example I've got this error:

error: no matching function for call to 'StringStream::StringStream(String*)'
StringStream stream(&output);

Any idea?

Thanks for your support!

@ricaun
Copy link

ricaun commented Jul 28, 2018

@jarija I have the same error

To fix I change the StringStream stream(&output); to StringStream stream((String &)output);

Try this example

#include "StringStream.h"

void setup() {
  Serial.begin(9600);
  String output;
  StringStream stream((String &)output);
  testStream(stream);
  Serial.println(output);
}

void loop() {}

void testStream(Stream &out) {
  for(int i = 0; i< 10; i++){
    out.print("testStream ");
    out.println(i);
  }
}

@arcao
Copy link
Author

arcao commented Oct 16, 2023

Fixed.

@jarija
Copy link

jarija commented Oct 16, 2023

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment