toJson method
The toJson() method serializes the model's layers and their contents into a JSON format.
Each layer's type, number of neurons, neuron names, values, and edge weights are included in the JSON string, which is returned when the operation is complete.
Implementation
Future<String> toJson() async {
Map<String, Map<String, String>> layerMap = {};
int index = 0;
for (Layer layer in layers) {
layerMap[index.toString()] = {
'layerType': jsonEncode(layer.layerType.name),
'neuronCount': jsonEncode(layer.neurons.length),
'neuronNames':
jsonEncode(layer.neurons.map((neuron) => neuron.name).toList()),
'neuronValues':
jsonEncode(layer.neurons.map((neuron) => neuron.value).toList()),
'neuronOutputEdgeWeights': jsonEncode(layer.neurons
.map((neuron) =>
neuron.outputEdges?.map((edge) => edge.weight).toList())
.toList()),
'neuronInputEdgeWeights': jsonEncode(layer.neurons
.map((neuron) =>
neuron.inputEdges?.map((edge) => edge.weight).toList())
.toList()),
'neuronOldValues':
jsonEncode(layer.neurons.map((neuron) => neuron.oldValue).toList()),
'neuronError':
jsonEncode(layer.neurons.map((neuron) => neuron.error).toList()),
};
index += 1;
}
String jsonString = jsonEncode(layerMap);
return jsonString;
}