hiranthaR / Json-to-Dart-Model

Json to Dart Model extension can convert JSON objects into Dart data classes. It supports pure Dart class conversion, Flutter-recommended JSON serialization using annotations, Freezed support, Effective Dart:Style, and many more features. Currently, it has more than 135,000 installs.
https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart
MIT License
93 stars 18 forks source link

To be closed as mistake of me #98

Closed nicolasmol closed 2 years ago

nicolasmol commented 2 years ago

Is there an existing issue for this?

Current Behavior

Running whatever JSON to DART model from Selection or from .json_models folder where I have the following .jsconc

Expected Behavior

Methods from both classes Question & Answer should be implemented but only the top level ones (Question) are:

class Question {
  final String? id;
  final String? title;
  final String? imageUrl;
  final String? description;
  final int? timeSeconds;
  final List<Question>? questions;

  const Question({
    this.id,
    this.title,
    this.imageUrl,
    this.description,
    this.timeSeconds,
    this.questions,
  });

  factory Question.fromJson(Map<String, dynamic> json) => Question(
        id: json['id'] as String?,
        title: json['title'] as String?,
        imageUrl: json['image_url'] as String?,
        description: json['Description'] as String?,
        timeSeconds: json['time_seconds'] as int?,
        questions: (json['questions'] as List<dynamic>?)
            ?.map((e) => Question.fromJson(e as Map<String, dynamic>))
            .toList(),
      );

  Map<String, dynamic> toJson() => {
        'id': id,
        'title': title,
        'image_url': imageUrl,
        'Description': description,
        'time_seconds': timeSeconds,
        'questions': questions?.map((e) => e.toJson()).toList(),
      };

  Question copyWith({
    String? id,
    String? title,
    String? imageUrl,
    String? description,
    int? timeSeconds,
    List<Question>? questions,
  }) {
    return Question(
      id: id ?? this.id,
      title: title ?? this.title,
      imageUrl: imageUrl ?? this.imageUrl,
      description: description ?? this.description,
      timeSeconds: timeSeconds ?? this.timeSeconds,
      questions: questions ?? this.questions,
    );
  }
}
class Answer {
  Answer();

  factory Answer.fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    throw UnimplementedError('Answer.fromJson($json) is not implemented');
  }

  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    throw UnimplementedError();
  }
}

THIS ONE SHOULD BE LIKE :

class Answer {
  final String? answer;
  final String? indentifier;

  const Answer({this.indentifier, this.answer});

  factory Answer.fromJson(Map<String, dynamic> json) => Answer(
        answer: json['answer'] as String?,
        indentifier: json['indentifier'] as String?,
      );

  Map<String, dynamic> toJson() => {
        'answer': answer,
        'indentifier': indentifier,
      };
}

Steps To Reproduce

No response

Version

3.5.8

Relevant JSON syntax

{
  "__className": "question",
  "__path": "/lib/app/data/models/question_paper_model",
  "id": "ppr004",
  "title": "Biology",
  "image_url": "",
  "Description": "Basic Biology Multiple Choice Questions (MCQ) to practice basic Biology quiz answers",
  "time_seconds": 900,
  "questions": [
    {
      "id": "ppr004q001",
      "question": "A relationship in which one animal hunts, kills and eats another",
      "answers": [
        {
          "identifier": "A",
          "Answer": "Symbiosis"
        },
        {
          "identifier": "B",
          "Answer": "Mutualism"
        },
        {
          "identifier": "C",
          "Answer": "Parasitism"
        },
        {
          "identifier": "D",
          "Answer": "Predation"
        }
      ],
      "correct_answer": "D"
    },
    {
      "id": "ppr004q002",
      "question": "The animal that is hunted and killed for food.",
      "answers": [
        {
          "identifier": "A",
          "Answer": "Decomposer"
        },
        {
          "identifier": "B",
          "Answer": "Predator"
        },
        {
          "identifier": "C",
          "Answer": "Scavenger"
        },
        {
          "identifier": "D",
          "Answer": "Prey"
        }
      ],
      "correct_answer": "D"
    },
    {
      "id": "ppr004q003",
      "question": "A close relationship between two different species of organisms living together.",
      "answers": [
        {
          "identifier": "A",
          "Answer": "Mutualism"
        },
        {
          "identifier": "B",
          "Answer": "Symbiosis"
        },
        {
          "identifier": "C",
          "Answer": "Competition"
        },
        {
          "identifier": "D",
          "Answer": "Commensialism"
        }
      ],
      "correct_answer": "B"
    },
    {
      "id": "ppr004q004",
      "question": "A symbiotic relationship in which both species benefit.",
      "answers": [
        {
          "identifier": "A",
          "Answer": "Mutualism"
        },
        {
          "identifier": "B",
          "Answer": "Commensialism"
        },
        {
          "identifier": "C",
          "Answer": "Parasitism"
        },
        {
          "identifier": "D",
          "Answer": "Predation"
        }
      ],
      "correct_answer": "A"
    },
    {
      "id": "ppr004q005",
      "question": "Which organelle is found in most plants, some bacteria and some protists?",
      "answers": [
        {
          "identifier": "A",
          "Answer": "ribosomes"
        },
        {
          "identifier": "B",
          "Answer": "mitochondria"
        },
        {
          "identifier": "C",
          "Answer": "endoplasmic reticulum"
        },
        {
          "identifier": "D",
          "Answer": "chloroplasts"
        }
      ],
      "correct_answer": "D"
    }
  ]
}

Anything else?

No response