toJSON (records, opts) {
    let record
    opts || (opts = {})
    if (utils.isArray(records)) {
      return records.map((record) => this.toJSON(record, opts))
    } else {
      record = records
    }
    const relationFields = (this ? this.relationFields : []) || []
    let json = {}

    // Copy properties defined in the schema
    if (this && this.schema) {
      json = this.schema.pick(record)
    } else {
      for (var key in record) {
        if (relationFields.indexOf(key) === -1) {
          json[key] = utils.plainCopy(record[key])
        }
      }
    }

    // The user wants to include relations in the resulting plain object representation
    if (this && opts.withAll) {
      opts.with = relationFields.slice()
    }
    if (this && opts.with) {
      if (utils.isString(opts.with)) {
        opts.with = [opts.with]
      }
      utils.forEachRelation(this, opts, (def, optsCopy) => {
        const relationData = def.getLocalField(record)
        if (relationData) {
          // The actual recursion
          if (utils.isArray(relationData)) {
            def.setLocalField(json, relationData.map((item) => {
              return def.getRelation().toJSON(item, optsCopy)
            }))
          } else {
            def.setLocalField(json, def.getRelation().toJSON(relationData, optsCopy))
          }
        }
      })
    }
    return json
  }
Return a plain object representation of the given record. Relations can
be optionally be included. Non-schema properties can be excluded.

@example
import { Mapper, Schema } from 'js-data';
const PersonMapper = new Mapper({
  name: 'person',
  schema: {
    properties: {
      name: { type: 'string' },
      id: { type: 'string' }
    }
  }
});
const person = PersonMapper.createRecord({ id: 1, name: 'John', foo: 'bar' });
// "foo" is stripped by toJSON()
console.log(PersonMapper.toJSON(person)); // {"id":1,"name":"John"}

const PersonRelaxedMapper = new Mapper({
  name: 'personRelaxed',
  schema: {
    properties: {
      name: { type: 'string' },
      id: { type: 'string' }
    },
    additionalProperties: true
  }
});
const person2 = PersonRelaxedMapper.createRecord({ id: 1, name: 'John', foo: 'bar' });
// "foo" is not stripped by toJSON
console.log(PersonRelaxedMapper.toJSON(person2)); // {"id":1,"name":"John","foo":"bar"}

@method Mapper#toJSON
@param {Record|Record[]} records Record or records from which to create a
POJO representation.
@param {object} [opts] Configuration options.
@param {string[]} [opts.with] Array of relation names or relation fields
to include in the POJO representation.
@param {boolean} [opts.withAll] Whether to simply include all relations in
the representation. Overrides `opts.with`.
@returns {Object|Object[]} POJO representation of the record or records.
@since 3.0.0