<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p><br>
</p>
<div class="moz-cite-prefix">El 17/6/24 a las 10:56, Mark Volkmann
escribió:<br>
</div>
<blockquote type="cite"
cite="mid:CAFfRWnXdVWVcL+bacuj3d-674ySU4c1ABfakcBoiE27RxBXyqQ@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">It occured to me that the reason the JSON package
does not define a method that generates JSON from an arbitrary
object is that it does not want to assume that including the
value of every instance variable is what you want. So it
requires you to implement the instance method <font
face="monospace">jsonWriteOn:</font>` in each of your custom
classes that need to be converted to a JSON string. </div>
</blockquote>
Yes. That's the common way of working with JSON.<br>
<blockquote type="cite"
cite="mid:CAFfRWnXdVWVcL+bacuj3d-674ySU4c1ABfakcBoiE27RxBXyqQ@mail.gmail.com">
<div dir="ltr">I think you also need to implement a class method
in each of those custom classes that takes a JSON string and
returns an object created from it. Here's an example. I'm
curious if anyone thinks this is not the intended use of the
JSON package.
<div><br>
</div>
<div>Feature require: 'JSON'<br>
</div>
<div><br>
</div>
<div>I have a class named <font face="monospace">VDog</font>
that contains the instance variables <font face="monospace">id</font>,
<font face="monospace">name</font>, and <font
face="monospace">breed</font>.</div>
<div><br>
</div>
<div>Here is my instance method to generate a JSON string:</div>
<div><br>
</div>
<div><font face="monospace">jsonWriteOn: aWriteStream<br>
{<br>
#id->id.<br>
#name->name.<br>
#breed->breed<br>
} asDictionary jsonWriteOn: aWriteStream</font><br>
</div>
<div><br>
</div>
<div>Here is my class method to create a <font face="monospace">VDog</font>
object from a JSON string.</div>
<div>It uses my class method <font face="monospace">id:name:breed:</font>
to create a new instance.</div>
<div><br>
</div>
<div><font face="monospace">fromJson: aString<br>
| jsonObject |<br>
jsonObject := Json readFrom: aString readStream.<br>
^VDog</font></div>
<div><font face="monospace"> id: (jsonObject at: #id)</font></div>
<div><font face="monospace"> name: (jsonObject at: #name)</font></div>
<div><font face="monospace"> breed: (jsonObject at:
#breed)</font><br>
</div>
<div><font face="monospace"><br>
</font></div>
<div><font face="arial, sans-serif">Would you do this any
differently?</font></div>
</div>
</blockquote>
<p><font face="arial, sans-serif">Your implementations are fine IMO.
I would consider working directly with streams instead of
strings in #fromJson: <br>
</font></p>
<div><font face="monospace">fromJson: aStream<br>
| jsonObject |<br>
jsonObject := Json readFrom: aStream</font></div>
<div><font face="monospace"> id: (jsonObject at: #id)</font></div>
<div><font face="monospace"> name: (jsonObject at: #name)</font></div>
<p><font face="monospace"> breed: (jsonObject at: #breed)</font></p>
<p><font face="monospace"><br>
</font></p>
<p><font face="monospace">But do what you think is better for your
program.</font></p>
<p><font face="monospace"><br>
</font></p>
<p><font face="monospace"> Mariano<br>
</font></p>
<p></p>
</body>
</html>