Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some information about DataFormat #606

Closed
wants to merge 15 commits into from
Closed

Added some information about DataFormat #606

wants to merge 15 commits into from

Conversation

johnfg2610
Copy link

Since i recently learned about this and though it would be useful for others i decided to put it in the docs


DataFormat
==========
A alternative to DataTranslators is using :javadoc:`DataFormat.readFrom` or :javadoc:`DataFormat.writeTo` which allows you to convert a data container this allows you to store a data container as a hocon, json or even NBT you can of course retrive data using data format.
Copy link
Member

@ST-DDT ST-DDT Jun 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to add your class to the javadoc-import block at the top of the page. Also I'm not sure whether you can/should reference methods like this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh i see thank you and i only named the methods like that because i was guessing with the error it has been corrected :)


.. code-block:: java

public Optional<DataContainer> fromJsonFormat(InputStream inputStream){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an odd example considering its just wrapping the dataformat to squash the error and returns a null instead of an optional (!).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :)

Copy link
Member

@Inscrutable Inscrutable left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the corrections, we also need to know which SpongeAPI versions this PR belongs to.


DataFormat
==========
A alternative to DataTranslators is using :javadoc:`DataFormat` which allows you to convert a data container this allows you to store a data container as a hocon, json or even NBT you can of course retrive data using data format.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is a bit wonky, and far too long. How about this:
"An alternative to DataTranslators is to use :javadoc:DataFormat, which allows you to store a data container as a hocon, json or NBT file. You can also retrieve data using DataFormat."
It is also preferable to format class names etc like this, wherever they appear.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :)

==========
A alternative to DataTranslators is using :javadoc:`DataFormat` which allows you to convert a data container this allows you to store a data container as a hocon, json or even NBT you can of course retrive data using data format.

This is very useful if your for example using a database to store information as you can then serialize any data container to for example json format
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your -> you are (or you're)
you can use e.g. instead of "for example"
it needs at least one comma, preferably after "information"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :)

@johnfg2610
Copy link
Author

Json is new in sponge API 6 but hocon and NBT have been in for a while basically since the beginning but uses different methods prior to API 5
so this is for Sponge API 5-7

@Inscrutable Inscrutable added this to the v5.0.0 milestone Jun 7, 2017

DataFormat
==========
An alternative to DataTranslators is to use :javadoc:DataFormat, which allows you to store a ``DataContainer`` as a hocon, json or NBT file. You can also retrieve data using ``DataFormat``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HOCON and JSON should be written in capital letters, just like NBT

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)


.. code-block:: java

public OutputStream toJsonFormat(Player player){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to use a specific example, like writing a ItemStack to a file and loading it back from it. You use an arbitrary ByteArrayOutputStream here which is returned as OutputStream so the return value of this method is essentially useless without additional casting. Additionally, how often do you actually serialize an entire Player on your own, and how would you load it back? The example below does only show how to read it back into a DataContainer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will use itemstack but i havnt written it to a file because that isnt really what i would expect people to do with it there more likly to use DataTranslators for that i was more thinking this would be used for databases etc also im not 100% sure how to go from datacontainer to the orignal object havnt been able to work that out yet


DataFormat
==========
An alternative to DataTranslators is to use :javadoc:DataFormat, which allows you to store a ``DataContainer`` as a HOCON, JSON or NBT file. You can also retrieve data using ``DataFormat``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't necessarily create files. It transforms DataContainers into HOCON, JSON or NBT formats, which could be converted into a file, but in your example below, you don't create a file.

That last sentence should read something like:

You can also re-create DataContainers using these DataFormats.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

==========
An alternative to DataTranslators is to use :javadoc:DataFormat, which allows you to store a ``DataContainer`` as a HOCON, JSON or NBT file. You can also retrieve data using ``DataFormat``.

This is very useful if you're for example using a database to store information, as you can then serialize any ``DataContainer`` to json format.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few instances in your commit still where you need to update "json" to JSON. Also, this sentence doesn't seem like it fits well - it might be better to say something like:

For example, you can use DataFormat to create a JSON representation of a DataContainer, which can then be easily stored in a database as JSON. DataFormat can then be used to rebuild the DataContainer from this JSON when required.

It's possible it could be worded better than that, but this sentence doesn't read well as it is.

return outputStream;
}

**Code Example: Serializing a ItemStack from json format**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not what this example is - it's deserializing into a DataContainer from JSON.

I think what Deamon was getting at is this full example doesn't really tell you much, all you are doing is wrapping the result in an optional, returning an empty Optional if it fails. It doesn't tell you much - you might be better served showing how you then could use the resultant DataContainer, perhaps showing how to re-create the ItemStack from the container? That would fit nicely with your example above - it's showing that the returned DataContainer could be a valid ItemStack - which it would be in the context of the previous example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok ive learned how to do this now and will add it to the example shortly

Copy link
Contributor

@dualspiral dualspiral left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @johnfg10 for listening to our suggestions and taking the time to do this and share your knowledge. I look forward to seeing this being merged.

Copy link
Member

@gabizou gabizou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deserialization isn't taking advantage of a few quality of life shortcuts that exist on the objects already being used.


}
public static Optional<ItemStackSnapshot> getItemStackSnapshotFromDataContainer(DataContainer dataContainer){
final Optional<DataBuilder<ItemStackSnapshot>> dataBuilderOptional = Sponge.getDataManager().getBuilder(ItemStackSnapshot.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the data manager stuff is unnecessary, all you need to do is call dataContainer.getSerializable(ItemStackSnapshot.class, query) or however the parameters are ordered.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i tried doing that in my plugin and it didnt work correctly for me so i went with this method(as i know it works) although i will retest and make sure

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well i suppose i must of used it incorrectly the first time hmm il update the example shortly

final Optional<DataBuilder<ItemStackSnapshot>> dataBuilderOptional = Sponge.getDataManager().getBuilder(ItemStackSnapshot.class);
if (dataBuilderOptional.isPresent()){
DataBuilder<ItemStackSnapshot> itemStackSnapshotDataBuilder = dataBuilderOptional.get();
Optional<DataView> dataViewOptional = dataContainer.getView(DataQuery.of("item"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are you getting the item query from?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed now sry forgot to remove it i pulled that example out of one of my plugins

return outputStream;
}

**Code Example: Deserializing a ItemStackSnapshot from json format**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I know it's been discussed before that you want to show how to use the DataContainer resulting from being converted from JSON, but I feel that it should still be shown how to convert it from JSON to a DataContainer, @dualspiral, I'm open for suggestions. I don't want to make it back and forth, but the wording is a little awkward since you state here that it's deserializing from JSON format, but there's no mention of JSON.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the original thought of mine is that while it doesn't matter really what we deserialise to, having an example in context is often beneficial - an ItemStackSnapshot is a classic example of what you might want to serialise/deserialise. Right now, this code example is not useful, because it now skips the step of exactly what we want to show.

Effectively, the goal is to say "hey, if you don't want to serialise your DataContainer into configuration, then DataFormat is for you". I hate to say this, but many developers learn by example, and may be stuck on the point that ByteArrayOutputStreams and input streams are here, I suspect some don't know what they do and is going to detract from the actual goal. I also expect most developers will use this for databases, and just want to get a string out to save into the database.

On the flip side, saying "here is a data container, here is JSON, here is the container again" is not going to wash either - why would a developer care about a simple data container?

So, how about, rather than providing two examples, we consider providing a sample class here that developers can use that is essentially a template on how we might want to store and retrieve a data container backed object? Something like JsonItemStackSnapshotConverter (or whatever you like) that contains two commented methods, toJson and fromJson, that illustrates how to get a String from an ItemStackSnapshot, and vice versa. That way, we get the context of when we might use this (item serialisation), and how this would be performed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Inscrutable
Copy link
Member

We still have 2 pending reviews here. Can we get this cleaned up and merged soon?


.. code-block:: java

public ByteArrayOutputStream toJsonFormat(ItemStackSnapshot itemStack){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a space before {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think it would be easier to understand if you don't return the ByteArrayOutputStream, but rather use a specific example like writing a ItemStackSnapshot to a file. I don't think anyone would use the method like this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in the new pr


public ByteArrayOutputStream toJsonFormat(ItemStackSnapshot itemStack){
DataContainer playerDataContainer = itemStack.toContainer();
DataFormat dataFormat = DataFormats.JSON;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes any sense to store this in a variable first, just access it directly using DataFormats.JSON.writeTo below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in the new pr

@johnfg2610
Copy link
Author

ive had to open a new pull request as i apparently deleted the fork as some point

@johnfg2610 johnfg2610 closed this Sep 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants