rcdmk / aspJSON

A fast classic ASP JSON parser and encoder for easy JSON manipulation to work with the new JavaScript MV* libraries and frameworks.
MIT License
203 stars 89 forks source link

1 record in a RecordSet #5

Closed dtrillo closed 8 years ago

dtrillo commented 8 years ago

Hi. I wonder if you can add more extra features to your project. In this case, it's something that I am using when a recordset only has one record. I want to have a JSON object with the data of that record, and not having a collection of data. The code I'm using is:

function ajustaJSON(str) ajusta = replace(str, "[", "") ajustaJSON = replace(ajusta, "]", "") end function

function Recordset1reg2JSON(AdoRS) Dim oJSONarr set oJSONarr = new JSONarray oJSONarr.LoadRecordset AdoRS Recordset1reg2JSON = ajustaJSON(oJSONarr.Serialize()) end Function

This two functions are used like this:

Dim oJSON set oJSON = new JSONobject jsonString = Recordset1reg2JSON(AdoRS) oJSON.Parse(jsonString)

Now, oJSON has several properties that match the fields of the record, with their values!

It could be great if you could add a new feature similar to LoadRecordSet that can do all this step in only one function. You don't need to ask for a property name, becuase the property names are the fields names of the recordset!

Thanks in advance!

rcdmk commented 8 years ago

Hi. You can simply get the element from the array:

Dim oJSONarr
set oJSONarr = new JSONarray
oJSONarr.LoadRecordset AdoRS

Dim items, oJSON
items = oJSONarr.items
set oJSON = items(0)

I should implement an easier way to get items by index soon. It will be something like:

Dim items, oJSON
set oJSON = oJSONarr(0)

Is this enough to help?

rcdmk commented 8 years ago

See issue #6, as it will be closed when the new funcionality become available.

dtrillo commented 8 years ago

Thanks for the tip #6! As I told you, I sometimes know already that a recordset has only ONE record, and that's the way I thought I can do it. I wonder why you cann't use oJSONarr inside oJSON object, so you can to it in one step, and not using two objects. I know that it suppose to mix classes!

rcdmk commented 8 years ago

I could implement a property or a method to load only the first record and return the object instead of an array.

The concern about the 2 classes is that they represent different things. One represent an object and the other a collection. Each one has ist's own responsabilities. Although not perfect, it's somewhat conformant with the SRP in SOLID.

rcdmk commented 8 years ago

I've implemented the LoadFirstRecord(rs) in jsonObject class. With this, you get exactly what you asked for:

jsonObj.loadFirstRecord myRecordSet

response.write jsonObj("id") ' outputs: 1

I hope you like it.