在这种情况下如何从 json 解码?

时间:2021-06-07 03:35:34

标签: json dart parse-platform back4app

我将整个类保存为 Parse Back4App 服务器上的 json 对象。将对象解析为 json 很痛苦,因为我需要将嵌套类 Book 和主类 DatabaseSyncItem 中的许多属性转换为字符串。现在我不知道如何将这个 json 对象转换回 DatabaseSyncObject。我认为将嵌套的 json book 对象恢复到对象模型也是如此,也就是说它不会以这种方式工作。

我收到错误,但没有成功。如果我尝试从 main 中的 json 对象列表中访问特定索引,我可以找到某个地方,但这非常混乱。在这个情况下,你会怎么做?帮助我,我第一次使用 json。

尝试应用这些帖子但没有工作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "lz4.h"




int main()
{
    time_t start,end;
    int compress, decompress;
    int num_f=0;
    int i;

    const char* const src;
    int srcSize=16385;
    int dstCapacity = LZ4_compressBound(srcSize);
    char* compressed_data = malloc((size_t)dstCapacity);


    FILE * fc;


    num_f = splitFile("/home/ziruo/research/test.txt",16384);

    start = time(NULL);
    for( i = 1; i <= num_f; i++)
    {

        fc = fopen(("/home/ziruo/research/text.txt.%03d",i),"r");
        fgets(src,16384,fc);
        printf("%s",src);
        LZ4_compress_default(src,compressed_data,srcSize,dstCapacity);

    }
    //LZ4LIB_API int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity);

    end = time(NULL);

    compress=difftime(end,start);
    printf("%d",compress);
    //printf("%d\n",splitFile("/home/ziruo/research/test.txt",16384));




    return 0;

1 个答案:

答案 0 :(得分:0)

似乎这个答案比我最初预期的要复杂得多。需要使用json可序列化包和json注解包。

包是必要的,当您查看用于处理类型、null 和嵌套类的生成代码时,您就会明白为什么。以防其他人遇到这种情况。

import 'package:hive/hive.dart';
import 'package:json_annotation/json_annotation.dart';
part 'book_model.g.dart';

@JsonSerializable(includeIfNull: true)
@HiveType(typeId: 0)
class Book {
  @HiveField(0)
  String title;

  @HiveField(1)
  String author;

  @HiveField(2)
  DateTime publishingDate;

  @HiveField(3)
  DateTime dateAdded;

  @HiveField(4)
  DateTime lastModified;

  Book({
    required this.title,
    required this.author,
    required this.publishingDate,
    required this.dateAdded,
    required this.lastModified,
  });

  factory Book.fromJson(Map<String, dynamic> json) => _$BookFromJson(json);
  Map<String, dynamic> toJson() => _$BookToJson(this);

  @override
  String toString() {
    return '''
  title: $title
  author: $author
  publishingDate: $publishingDate
  dateAdded: $dateAdded
  lastModified $lastModified
  ''';
  }
}

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'book_model.dart';

// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************

class BookAdapter extends TypeAdapter<Book> {
  @override
  final int typeId = 0;

  @override
  Book read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int, dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return Book(
      title: fields[0] as String,
      author: fields[1] as String,
      publishingDate: fields[2] as DateTime,
      dateAdded: fields[3] as DateTime,
      lastModified: fields[4] as DateTime,
    );
  }

  @override
  void write(BinaryWriter writer, Book obj) {
    writer
      ..writeByte(5)
      ..writeByte(0)
      ..write(obj.title)
      ..writeByte(1)
      ..write(obj.author)
      ..writeByte(2)
      ..write(obj.publishingDate)
      ..writeByte(3)
      ..write(obj.dateAdded)
      ..writeByte(4)
      ..write(obj.lastModified);
  }

  @override
  int get hashCode => typeId.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is BookAdapter &&
          runtimeType == other.runtimeType &&
          typeId == other.typeId;
}

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

Book _$BookFromJson(Map<String, dynamic> json) {
  return Book(
    title: json['title'] as String,
    author: json['author'] as String,
    publishingDate: DateTime.parse(json['publishingDate'] as String),
    dateAdded: DateTime.parse(json['dateAdded'] as String),
    lastModified: DateTime.parse(json['lastModified'] as String),
  );
}

Map<String, dynamic> _$BookToJson(Book instance) => <String, dynamic>{
      'title': instance.title,
      'author': instance.author,
      'publishingDate': instance.publishingDate.toIso8601String(),
      'dateAdded': instance.dateAdded.toIso8601String(),
      'lastModified': instance.lastModified.toIso8601String(),
    };
import 'package:json_annotation/json_annotation.dart';
import 'book_model.dart';
import 'package:hive/hive.dart';
part 'database_sync_model.g.dart';

@JsonSerializable(includeIfNull: true)
@HiveType(typeId: 1)
class DatabaseSyncItem {
  @HiveField(0)
  Book? previousBookValue;

  @HiveField(1)
  Book? updatedBookValue;

  @HiveField(2)
  DateTime dateAdded;

  @HiveField(3)
  DateTime lastModified;

  @HiveField(4)
  DatabaseAction entryAction;

  DatabaseSyncItem({
    this.previousBookValue,
    this.updatedBookValue,
    required this.dateAdded,
    required this.lastModified,
    required this.entryAction,
  });

  factory DatabaseSyncItem.fromJson(Map<String, dynamic> json) => _$DatabaseSyncItemFromJson(json);
  Map<String, dynamic> toJson() => _$DatabaseSyncItemToJson(this);

  @override
  String toString() {
  return '''
  previousValue: $previousBookValue
  updatedValue: $updatedBookValue
  dateAdded: $dateAdded
  lastModified: $lastModified
  entryAction: $entryAction
  ''';
   }
}

enum DatabaseAction {
  create,
  update,
  delete,
}

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'database_sync_model.dart';

// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************

class DatabaseSyncItemAdapter extends TypeAdapter<DatabaseSyncItem> {
  @override
  final int typeId = 1;

  @override
  DatabaseSyncItem read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int, dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return DatabaseSyncItem(
      previousBookValue: fields[0] as Book?,
      updatedBookValue: fields[1] as Book?,
      dateAdded: fields[2] as DateTime,
      lastModified: fields[3] as DateTime,
      entryAction: fields[4] as DatabaseAction,
    );
  }

  @override
  void write(BinaryWriter writer, DatabaseSyncItem obj) {
    writer
      ..writeByte(5)
      ..writeByte(0)
      ..write(obj.previousBookValue)
      ..writeByte(1)
      ..write(obj.updatedBookValue)
      ..writeByte(2)
      ..write(obj.dateAdded)
      ..writeByte(3)
      ..write(obj.lastModified)
      ..writeByte(4)
      ..write(obj.entryAction);
  }

  @override
  int get hashCode => typeId.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is DatabaseSyncItemAdapter &&
          runtimeType == other.runtimeType &&
          typeId == other.typeId;
}

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

DatabaseSyncItem _$DatabaseSyncItemFromJson(Map<String, dynamic> json) {
  return DatabaseSyncItem(
    previousBookValue: json['previousBookValue'] == null
        ? null
        : Book.fromJson(json['previousBookValue'] as Map<String, dynamic>),
    updatedBookValue: json['updatedBookValue'] == null
        ? null
        : Book.fromJson(json['updatedBookValue'] as Map<String, dynamic>),
    dateAdded: DateTime.parse(json['dateAdded'] as String),
    lastModified: DateTime.parse(json['lastModified'] as String),
    entryAction: _$enumDecode(_$DatabaseActionEnumMap, json['entryAction']),
  );
}

Map<String, dynamic> _$DatabaseSyncItemToJson(DatabaseSyncItem instance) =>
    <String, dynamic>{
      'previousBookValue': instance.previousBookValue,
      'updatedBookValue': instance.updatedBookValue,
      'dateAdded': instance.dateAdded.toIso8601String(),
      'lastModified': instance.lastModified.toIso8601String(),
      'entryAction': _$DatabaseActionEnumMap[instance.entryAction],
    };

K _$enumDecode<K, V>(
  Map<K, V> enumValues,
  Object? source, {
  K? unknownValue,
}) {
  if (source == null) {
    throw ArgumentError(
      'A value must be provided. Supported values: '
      '${enumValues.values.join(', ')}',
    );
  }

  return enumValues.entries.singleWhere(
    (e) => e.value == source,
    orElse: () {
      if (unknownValue == null) {
        throw ArgumentError(
          '`$source` is not one of the supported values: '
          '${enumValues.values.join(', ')}',
        );
      }
      return MapEntry(unknownValue, enumValues.values.first);
    },
  ).key;
}

const _$DatabaseActionEnumMap = {
  DatabaseAction.create: 'create',
  DatabaseAction.update: 'update',
  DatabaseAction.delete: 'delete',
};