<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Encoding JSON array of an array of floats</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/95645/encoding-json-array-of-an-array-of-floats</link><description>Hi, 
 I&amp;#39;m developing Software for the nRF52840. I&amp;#39;m using NCS version 2.2.0 and Zephyr OS build v3.2.99-ncs1. 
 I try to encode a JSON message that will be send via MQTT. 
 I managed to encode, objects, strings and other primitives, 
 But now I&amp;#39;m blocked</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 30 Jan 2023 10:20:16 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/95645/encoding-json-array-of-an-array-of-floats" /><item><title>RE: Encoding JSON array of an array of floats</title><link>https://devzone.nordicsemi.com/thread/407058?ContentTypeID=1</link><pubDate>Mon, 30 Jan 2023 10:20:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5d45564e-45d9-4917-b643-e279a67700dd</guid><dc:creator>Jorcis</dc:creator><description>&lt;p&gt;Great! Thanks Sigurd!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Encoding JSON array of an array of floats</title><link>https://devzone.nordicsemi.com/thread/407028?ContentTypeID=1</link><pubDate>Mon, 30 Jan 2023 09:18:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2f2509d4-fa1b-4e93-9bc3-82c501840806</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;Hi Jordi,&lt;/p&gt;
&lt;p&gt;Good to hear that you found JSON_TOK_FLOAT!&lt;/p&gt;
&lt;p&gt;I do not know if it runs faster, but it ups readability to use &amp;quot;sprintf&amp;quot;:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;

static float vector[3][2] = {
    {2.34, 0.64},
    {3.41, 0.67},
    {2.56, 0.59}
};


void encode_aray_array_float(int M, int N, float data[M][N], char* buf_ini){

    sprintf(buf_ini, &amp;quot;&amp;quot;); // Initialize buf_init to be empty
    for(int m=0; m&amp;lt;M; m++){
        for(int n=0; n&amp;lt;N; n++){
            if(n==N-1){
                sprintf(buf_ini, &amp;quot;%s [%.2lf];&amp;quot;, buf_ini, data[m][n]);
            }
            else {
                sprintf(buf_ini, &amp;quot;%s [%.2lf],&amp;quot;, buf_ini, data[m][n]);
            }
        }
    }
}


void main(void)
{
    char test_string[200];
    encode_aray_array_float(3, 2, vector, test_string);
    printk(&amp;quot;%s\n&amp;quot;, test_string);
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;and prj.conf:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;# To make sprintf work with floats
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

# To make printk work with floats
CONFIG_CBPRINTF_FP_SUPPORT=y
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Sigurd Hellesvik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Encoding JSON array of an array of floats</title><link>https://devzone.nordicsemi.com/thread/406855?ContentTypeID=1</link><pubDate>Fri, 27 Jan 2023 13:20:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7abd6944-866a-42ab-a4a7-c9fa7fb65f3f</guid><dc:creator>Jorcis</dc:creator><description>&lt;p&gt;Hi Sigurd,&lt;/p&gt;
&lt;p&gt;It&amp;#39;s OK, I finally make it.&lt;/p&gt;
&lt;p&gt;As JSON_TOK_FLOAT, can accept any formatted string, I first encode the double array of floats and then I lie the json_obj_token.start to point to that &amp;quot;fake&amp;quot; string.&lt;/p&gt;
&lt;p&gt;I insert a code example here:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;kernel.h&amp;gt;
#include &amp;lt;data/json.h&amp;gt;

static float vector[3][2] = {
	{2.34, 0.64},
	{3.41, 0.67},
	{2.56, 0.59}
};

struct json_data{
	struct json_obj_token vector;
	char arr_arr_floats[100];
};

static struct json_obj_descr parent_descr[] = {
	JSON_OBJ_DESCR_PRIM(struct json_data, vector, JSON_TOK_FLOAT)
};

size_t encode_aray_array_float(float** data, size_t M, size_t N, char* buf_ini)
{
	char* buf = buf_ini;

	*buf++ = &amp;#39;[&amp;#39;;
	for(int i = 0; i &amp;lt; M; i++){
		*buf++ = &amp;#39;[&amp;#39;;
		for(int j = 0; j &amp;lt; N; j++){
			gcvt(data[i][j], 6, buf);
			while(*buf != &amp;#39;\0&amp;#39;) buf++;
			if(j &amp;lt; N - 1){
				*buf++ = &amp;#39;,&amp;#39;;
			}
		}
		*buf++ = &amp;#39;]&amp;#39;;
		if(i &amp;lt; M - 1){
			*buf++ = &amp;#39;,&amp;#39;;
		}
	}
	*buf++ = &amp;#39;]&amp;#39;;
	*buf = &amp;#39;\0&amp;#39;;
	return (buf - buf_ini);
}

int main()
{
	struct json_data data;

	/** Prepare JSON data */
	data.vector.length = encode_aray_array_float((float**)vector, 3, 2, data.arr_arr_floats);
	data.vector.start = data.arr_arr_floats;

	/** Encode the json obj */
	char json[200];
	int err = json_obj_encode_buf(parent_descr, ARRAY_SIZE(parent_descr), &amp;amp;data, json, sizeof(json));
	printk(&amp;quot;err = %d\n&amp;quot;, err);
	printk(&amp;quot;%s\n&amp;quot;, json);

	while(1){
		k_msleep(1000);
	}
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I wonder if there is a more elegant way to do it.&lt;/p&gt;
&lt;p&gt;I close the case, many thanks for your attention.&lt;/p&gt;
&lt;p&gt;Jordi.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Encoding JSON array of an array of floats</title><link>https://devzone.nordicsemi.com/thread/404793?ContentTypeID=1</link><pubDate>Fri, 13 Jan 2023 12:39:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:415b5017-611b-4019-88ec-0cf7b9f836f6</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;Hi Jordi,&lt;/p&gt;
&lt;p&gt;The JSON library is likely using a zephyr library.&lt;br /&gt;In that case I think you will get better help on the &lt;a href="https://www.zephyrproject.org/contact-us/"&gt;Zephyr Discord&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;But if you upload some test code here, I can have a look at it and see if I can help.&lt;br /&gt;(Insert-&amp;gt;File) or (Insert-&amp;gt;Code)&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Sigurd Hellesvik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>