Visitar URL original
How to make base model · Issue #636 · jsonmodel/jsonmodel · GitHub
Skip to content

How to make base model  #636

Open
Open

Description

@HarunBahcel

Problem is I have one main model. I have two sub-models

@interface UserModel : JSONModel
@property (nonatomic) NSInteger  Id;
@property (nonatomic) NSInteger  SponsorId;
@end
@interface FinanceModel : JSONModel
@property (nonatomic) NSInteger  Id;
@property (nonatomic) NSInteger  Money;
@end
@interface BaseModel : JSONModel
@property KullaniciModel *Result;
@property NSInteger  Status;
@property NSString  *ErrorMessage;
@end
@interface BaseModel : JSONModel
@property FinanceModel *Result;
@property NSInteger  Status;
@property NSString  *ErrorMessage;
@end

How do I get a Single Base Model?
for example

@interface BaseModel : JSONModel
@property JsonModel <BaseClass>   *Result;
@property NSInteger  Status;
@property NSString  *ErrorMessage;
@end

UserModel Json

{
  "Result": {
    "Id": 12312312312,
    "SponsorId": 0,
  },
  "Status": "1",
  "ErrorMessage": ""
}

FinanceModel Json

{
  "Result": {
    "Id": 12312312312,
    "Money": 110.00,
  },
  "Status": "1",
  "ErrorMessage": ""
}

How do I import a User and Finance model in a Base model

These are just examples. I have 50 sub-models and I need to create a Base Model for all of them.
Help Me?

Activity

  1. stevendesu commented on Nov 25, 2019

    @stevendesu

    Judging from your data, I might try something like:

    @interface BaseModel : JSONModel
    @property NSString *Status;
    @property NSString *ErrorMessage;
    @end
    
    @interface UserModel : BaseModel
    @property UserSubModel *Result;
    @end
    
    @interface UserSubModel : JSONModel
    @property NSInteger *Id;
    @property NSInteger *SponsorId;
    @end
    
    @interface FinanceModel : BaseModel
    @property FinanceSubModel *Result;
    @end
    
    @interface FinanceSubModel : JSONModel
    @property NSInteger *Id;
    @property NSInteger *Money;
    @end
    

    It may not be ideal because BaseModel doesn't make it clear that a Result field will always exist, but since Result can take two different types this is the only way I know to do this with JSONModel. Often when working with de-serialization libraries (JSONModel, GSON, Newtonsoft, etc) you need consistency in your JSON or it leads to complex workflows and hacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions