Architecture for to-string conversion

Aperta
#119 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
c

Direzione di ricerca

Inizia dal percorso attuale json_object-to-string e dalla relativa implementazione di struct printbuf, quindi esamina i punti di ingresso proposti fjson_object_write e fjson_object_write_ex. Il lavoro è completato quando le callback dell’utente possono ricevere l’output serializzato, il comportamento esistente di printbuf può essere costruito sull’API delle callback e le conversioni concorrenti non dipendono più da un buffer di output di proprietà dell’oggetto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Hi Rainer,

We have some issues with converting the json tree to a string from multiple threads at the same time. I think the problem is with the printbuf member that is being used by two threads. We will probably be able to work around this, but after looking at the code, I think it is a peculiar design that the output buffer is part of the json_object.

What would you think of adding the following functions to libfastjson:

/**
 * Signature of a custom print function that can be implemented by the user.
 * @param ptr user-supplied pointer
 * @param buffer buffer to be written
 * @param size size of the buffer
 * @return size_t number of bytes written
 */
typedef size_t (fjson_print_fn)(void *ptr, const char *buffer, size_t size);

/**
 * Write the json tree to a user-supplied function
 * @param jso the fjson_object instance
 * @param func the user supplied function to which all data will be passed
 * @param ptr user-supplied pointer that is passed as first param to the callback
 * @returns the number of bytes written
 */
extern size_t fjson_object_write(struct fjson_object *jso, fjson_print_fn *func, void *ptr);

/**
 * Extended write function that allows extra flags to be passed
 * @param jso the fjson_object instance
 * @param flags formatting options, see FJSON_TO_STRING_PRETTY and other constants
 * @param func the user supplied function to which all data will be passed
 * @param ptr user-supplied pointer that is passed as first param to the callback
 * @returns the number of bytes written
 */
extern size_t fjson_object_write_ex(struct fjson_object *jso, int flags, fjson_print_fn *func, void *ptr);

This would allow users to supply their own output function, for example to write the json to a FILE*, or to write it to a fixed size buffer on the stack or to a dynamically growing buffer just like the current "struct printbuf" does. In fact, the "printbuf" implementation could be implemented on top of these functions.

This will often also be a performance improvement because now we're always stuck with the dynamically growing printbuf -- even if we have a pretty good idea how big the output is going to be, or when we want to write to a file instead of to a dynamically allocated buffer.

Are you willing to accept a pull request that implements this?

Lingua principale
C
Stelle
61
Fork
34
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rsyslog/libfastjson

Tutte le issue di rsyslog/libfastjson

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.