Cloud and Software Architecture, Soft skills, IOT and embedded
Creating Immutable Functional Style Dart Model Objects based on JSON or JSON schemas
Get link
Facebook
X
Pinterest
Email
Other Apps
Flutter is opinionated about using a functional programming model that includes immutable data objects. I've found that we tend to ignore that whole immutable thing when dealing with JSON data.
Projects can be loose when handling incoming JSON data and how it is accessed, sprinkling string-style map keys across the codebase. We can reduce that tendency by providing models to eliminate the string keys and mutable models to stop people from directly manipulating maps of strings.
Some languages are functional only. Other languages have no way of generating immutable objects. In those cases, we just end up with objects that can be easily converted into and out of JSON structures and strings.
I'm working in Dart right now so the rest of this will be Dart samples
Modeling Longitude and Latitude Using the JSON Schema
Let's pick something simple as an example. We're going to use this Latitude and Longitude JSON Schema that follows the schema definition style from http://json-schema.org/draft-06/schema#. A coordinate on the earth's surface is represented by a longitude and a latitude.
Making the objects immutable, unchangeable after creation.
We want to operate in a Functional Programming model with immutable objects. Enabling Make all properties required and Make all properties final results in. Note the added final and required.
classCoordinate {
finaldoublelatitude;
finaldoublelongitude;
Coordinate({
requiredthis.latitude,
requiredthis.longitude,
});
}
Simplify creating mutated copies of the data.
Information changes. This means we need a way of creating new model objects that have one or more property differences from the object they were created from.
We want to make it easy on the programmer so we enable Generate CopyWiththat creates a new instance by making a copy with the requested changes applied. Note the added CopyWith()method.
classCoordinate {
finaldoublelatitude;
finaldoublelongitude;
Coordinate({
requiredthis.latitude,
requiredthis.longitude,
});
CoordinatecopyWith({
double?latitude,
double?longitude,
}) =>
Coordinate(
latitude:latitude??this.latitude,
longitude:longitude??this.longitude,
);
}
We're all about serializing and deserializing our model with JSON
The next step is to add JSON serialization and deserialization support. quicktype can generate global functions or static methods on the class. I'm a classy sort of person so I'll do the latter by turning off the Types onlyselector. We get a class that can
Create a model instance from a JSON string.
Create a model instance from a Map<String, dynamic>
Create a JSON data structure from a model object instance.
Create a JSON string from a model object instance.
Real-world JSON schemas and modeling are a lot more complicated than this simple example. There are a variety of tools out there. At the time of this article, I'd recommend exploring JSON modeling including functional programming if your language supports it. I used https://app.quicktype.io but there are other tools out there.
I do a lot of my development and configuration via ssh into my Raspberry Pi Zero over the RNDIS connection. Some models of the Raspberry PIs can be configured with gadget drivers that let the Raspberry pi emulate different devices when plugged into computers via USB. My favorite gadget is the network profile that makes a Raspberry Pi look like an RNDIS-attached network device. All types of network services travel over an RNDIS device without knowing it is a USB hardware connection. A Raspberry Pi shows up as a Remote NDIS (RNDIS) device when you plug the Pi into a PC or Mac via a USB cable. The gadget in the Windows Device Manager picture shows this RNDIS Gadget connectivity between a Windows machine and a Raspberry Pi. The Problem Windows 11 and Windows 10 no longer auto-installs the RNDIS driver that makes magic happen. Windows recognizes that the Raspberry Pi is some type of generic USB COM device. Manually running W indows Update or Upd...
We have Verizon FIOS with cable TV service. I've never really paid attention to how the Verizon side is wired up until Verizon recently upgraded my FIOS router and tuner box. After breaking my TV tuner by disconnecting an " unneeded" connection, I created yet another diagram of how the FIOS connections work. This is a basic wiring diagram of the house network missing a bunch of devices. Verizon ONT The Verizon optical network terminal converts the optical connection into TV and network standard connections. The ONT is actually two boxes in my situation. One outside connects to the optical and one inside converts something into an Ethernet WAN connection. This results in me connecting a TV COAX and an Ethernet WAN. Verizon TV Tuner The Verizon TV tuner decodes and decrypts TV data that it receives over coax. The TV tuner must talk back to Verizon for any video control operations. It could talk back wireless, over an extra ethernet connection to back over th...
Meetings can be crazy expensive and demoralizing when they burn hours without generating results. Good design sessions, decision-making sessions problem-solving sessions start with the pre-meeting work. An empty meeting invitation is useless and a time drain. Invitees should decline them. A meeting without any context about the problem or prior decisions is going to fail or be way more expensive than it needs to be. Invitees should decline them. Invitations should always state the purpose, contain an agenda, describe the expected decisions that need to be made, and contain background content. Everyone has to do their part. Organizers must meet some minimum bar for meetings to have any value. Attendees must read the invitations and the background materials. There will be super secret projects where no agenda and no supporting information are provided. Those should be the exception rather than the rule. Click to Enlarge Productive meetings have inputs, proces...
Comments
Post a Comment