Will Newtonsoft JSON work on the Meadow using Beta 4.4?

So, I can’t get Newtonsoft.Json to work. (Added with NuGet) using VS2019 and Meadow OS Beta 4.4.

SerializeObject causes an Exception ,that it can’t find System.Xml.
meadow --ListFiles shows that System.Xml.Dll is installed on the board

string output = JsonConvert.SerializeObject( tc1 );

System.IO.FileNotFoundException: Could not load file or assembly ‘System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ or one of its dependencies.
File name: ‘System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’

Does anyone currently use Newtonsoft.Json?

Any advice would be appreciated.
Thanks, Paul.

Sorry Paul, not a helpful answer from me unfortunately. I have not tried NewtonSoft JSON on the Meadow, because I switched over to System.Text.Json as soon as it appeared in the .NET Core environment. I know this works on the Meadow in case you are looking for alternatives for now.

At a superficial level it works. I updated my meadow yesterday. I’ve been looking at copying over my google firebase messaging from a Xamarin app. The following code

                var data = new
                {
                    to = "/topics/alert",
                    notification = new
                    {
                        body = "Meadow sending FCM Notifications",
                        title = "Meadow"

                    }
                };

                var httpContent = JsonConvert.SerializeObject(data);
                var client = new HttpClient();
                Console.WriteLine(httpContent);

Spits out

                {"to":"/topics/alert","notification":{"body":"Meadow sending FCM Notifications","title":"Meadow"}}

I do have an issue where when I send the message I get an out of memory error. So I say it’s working superfically because I’ve only been playing with the code this morning and don’t know if that out of memory error is something to do with me not configuring the networking properly, or whether the Meadow can run Newtonsoft but then doesn’t have enough memory to do anything else useful

Thanks for the info I will try it to see if my problem goes away.

using .Net Framework 4.7.2 and Newtonsoft.Json 12.0.3 if that helps

I am seeing the same thing in my examination of the issue. If I put the Serialize as the first code to run, it works. However, the first time my app tries to allocate memory, it reports that there is no memory to allocate. If I have the Serialize later in my code (where I discovered the issue originally), that is where I get an exception for System.Xml Not found. I feel like I have hit a wall on my ability to resolve this issue. However, I’m hopeful someone else, like yourself, will track it down. Thank you for your response.

So I think I’ve got it working.

I think the basic issue is that the Newtonsoft JSON framework just takes up too much memory. When I moved to System.Text.Json it was interesting to watch how many dll’s were uninstalled from the Meadow

2>[26/01/2021 20:39:02]     Meadow successfully deleted 'Newtonsoft.Json.dll'                                                                        (00:00:00.1269271 since last.)
2>[26/01/2021 20:39:12]     Meadow successfully deleted 'System.Xml.Linq.dll'                                                                        (00:00:09.9979770 since last.)
2>[26/01/2021 20:39:22]     Meadow successfully deleted 'System.Runtime.Serialization.dll'                                                           (00:00:10.0623086 since last.)
2>[26/01/2021 20:39:32]     Meadow successfully deleted 'System.ServiceModel.Internals.dll'                                                          (00:00:10.0000605 since last.)
2>[26/01/2021 20:39:42]     Meadow successfully deleted 'System.Data.dll'                                                                            (00:00:09.9684865 since last.)
2>[26/01/2021 20:39:52]     Meadow successfully deleted 'System.Transactions.dll'                                                                    (00:00:09.9995034 since last.)
2>[26/01/2021 20:40:02]     Meadow successfully deleted 'System.EnterpriseServices.dll'                                                              (00:00:10.0004231 since last.)

Try installing the System.Text.Json package from nuget.

In the header of the class you want to replace

using Newtonsoft.Json;

with

using System.Text.Json;

then I have this code working


        public async Task<bool> SendNotification()
        {
            Console.WriteLine("Start Notification: " );
            try
            {
                string uri = "http://192.168.1.11:8002/api/MeadowLogs";

                Console.WriteLine("Build object: ");
                var data = new
                {
			LogData = "Meadow" 
		};

                Console.WriteLine("Serialize Data: ");
                 
                string httpContent = JsonSerializer.Serialize(data);
                Console.WriteLine(httpContent);
                Console.WriteLine("Build httpcontent: ");
                 var stringContent = new StringContent(httpContent);
                
                Console.WriteLine("Create http client: ");
                var client = new HttpClient();

                Console.WriteLine("Adding Headers: ");
                stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                
                Console.WriteLine("Sending Message: ");
                var response = await client.PostAsync(uri, stringContent).ConfigureAwait(false);
                
                var result = response.Content.ReadAsStringAsync();
                 if (response.IsSuccessStatusCode)
                 {
                  
                    return true;
                 }
                 else
                 {
                   Console.WriteLine(response.ReasonPhrase);
              
                     return false;
                 }  

  }
            catch (TaskCanceledException ex)
            {

                Console.WriteLine(ex.ToString());
                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

Lots of unnecessary Console.Writelogs in there which you can remove. The other issue I had is that most internet enabled REST API’s are https URLs. As the Meadow doesn’t support TLS yet, I had to write a local API server to test it with. But this runs now, can connect to the http server and gets a response back. Hopefully this helps

1 Like

Thank you for your help. It turns out that System.Text.Json is the way to go. It runs on the Meadow perfectly as you stated. Microsoft recommends using it over Newtonsoft.Json going into the future.

2 Likes

Thank you for your help. System.Text.Json runs on the Meadow perfectly, and I read that it is Microsoft’s preferred library going into the future. There is one difference that I ran into between it and Newtonsoft.Json. Newtonsoft.Json will allow serialize/desterilize of private fields in an object where System.Text.Json will not. I’m currently using JSON as a way to save/restore object state/configuration to “disk”, so that it will survive power cycles/program restarts. I had to rewrite some of my code to use public Properties and forgo the use of private fields. Not an issue for what I’m doing. Now, I am back to making forward progress!
Once again, Thank you so much for your help.

1 Like