Skip to content

How to get whole document? #19

@Deilan

Description

@Deilan

Is there a way to retrieve a whole document, not just some of it's properties or collections?

For a json file from a docs' sample:

{
  "selected_user": { "id": 1, "name": "Phil", "age": 40, "city": "NY" },
  "temperature": 23.45,
  "note": "this is a test"
}

How could one retrieve it a as a whole into an instance of a class?

public class Settings
{
    public User SelectedUser { get; set; }
    public double Temperature { get; set; }
    public string Note { get; set; }
}

Activity

changed the title [-]How to get whole document[/-] [+]How to get whole document?[/+] on Aug 7, 2018
ttu

ttu commented on Aug 8, 2018

@ttu
Owner

There is no way to retrieve whole JSON as a document/object.

Root level properties are documents, so if you want to retrieve settings as a single object, you have to put it under property:

{
  "settings" : { 
    "selected_user": { "id": 1, "name": "Phil", "age": 40, "city": "NY" },
    "temperature": 23.45,
    "note": "this is a test"
  }
}
Deilan

Deilan commented on Aug 8, 2018

@Deilan
Author

@ttu Thanks for the response! May I request a feature to retrieve a whole document then? :)

ttu

ttu commented on Aug 9, 2018

@ttu
Owner

Yes, I can add it.

How about if I just modify the GetItem-method, so if it is called with empty string as a key, then it would return the whole JSON?

var settings = _ds.GetITem<Settings>(string.Empty);
Deilan

Deilan commented on Aug 12, 2018

@Deilan
Author

@ttu There are 3 points why I think it should be done in a different way:

  1. "" (empty string) - is a valid JSON token's name. In other words { "": 0 } is a valid JSON. Hence we have an ambiguation between retrieving "" property and the whole document. Besides it could break backward compatibility.
  2. From the method's name it's unclear that it could be used to retrieve the whole document - by using special values or without them.
  3. From my point of view It violates SRP: the method is being utilized for two distinct things.

I'd say there should be a dedicated method for getting the whole document. Something like GetRoot or GetDocument. GetRoot seems more appropriate to me because at the outermost level JSON could be an array as well as an object.

ttu

ttu commented on Aug 13, 2018

@ttu
Owner

Thanks for the input and yes, you are correct.

First I was thinking also GetRoot-method, but because of lack of time, thought that adding functionality to GetItem would have been the fastest way to implement this. As you pointed out, it wouldn't have even worked that way.

Have to think this littlebit more, but maybe GetRoot has to return Root-object that can either have a DocumentCollection or a single item.

AbdulmueezEmiola

AbdulmueezEmiola commented on Aug 28, 2020

@AbdulmueezEmiola

Hello,
I saw this issue opened and I was wondering whether It has been fixed. If not, I will like to attempt to provide a solution to it

ttu

ttu commented on Aug 30, 2020

@ttu
Owner

Thanks @AbdulmueezEmiola! This hasn't been fixed/implemented yet. I started some solution, but then forgot this...

Pushed the implementation I had done to https://github.com/ttu/json-flatfile-datastore/tree/add-getroot. Commit: 0258e7e

Based on what you had in mind you can continue that or if you have some better idea, provide a new solution :)

MostHated

MostHated commented on Nov 5, 2020

@MostHated

If someone does provide a solution for this, it would be appreciated if this could be updated. I just started using this today, got it all worked out and implemented, only to find out I could not simply get all records. : /

added 2 commits that reference this issue on Feb 21, 2022
9d04821
75c3a53
linked a pull request that will close this issue on Feb 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @ttu@MostHated@Deilan@AbdulmueezEmiola

      Issue actions

        How to get whole document? · Issue #19 · ttu/json-flatfile-datastore