File indexing completed on 2024-05-12 15:43:25

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (C) 2012 Bernd Buschinski (b.buschinski@googlemail.com)
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Library General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Library General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Library General Public License
0016  *  along with this library; see the file COPYING.LIB.  If not, write to
0017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  *  Boston, MA 02110-1301, USA.
0019  *
0020  */
0021 
0022 #ifndef JSONSTRINGIFY_H
0023 #define JSONSTRINGIFY_H
0024 
0025 #include "ustring.h"
0026 #include "identifier.h"
0027 #include "CommonIdentifiers.h"
0028 
0029 #include <HashSet.h>
0030 
0031 #include <vector>
0032 
0033 namespace KJS
0034 {
0035 
0036 class JSValue;
0037 class ExecState;
0038 class JSObject;
0039 
0040 class JSONStringify
0041 {
0042 public:
0043     enum StringifyState {
0044         Success,
0045         FailedCyclic,
0046         FailedException,
0047         FailedStackLimitExceeded
0048     };
0049 
0050     JSONStringify(ExecState *exec, JSValue *replacer, JSValue *spacer);
0051 
0052     JSValue *stringify(ExecState *exec, JSValue *object, StringifyState &state);
0053 
0054 private:
0055     enum ReplacerType {
0056         Invalid,
0057         Function,
0058         Array
0059     };
0060 
0061     UString stringifyObject(KJS::ExecState *exec, KJS::JSValue *object, KJS::JSValue *propertyName, KJS::JSObject *holder);
0062     UString quotedString(ExecState *exec, const UString &string);
0063     UString stringifyValue(ExecState *exec, JSValue *object, JSValue *propertyName, JSObject *holder);
0064 
0065     bool isWhiteListed(const Identifier &propertyName);
0066 
0067     StringifyState m_state;
0068     ReplacerType m_replacerType;
0069     JSObject *m_replacerObject;
0070     WTF::HashSet<Identifier> m_whitelistNames;
0071     UString m_spacer;
0072 
0073     //Object Stack for cyclic detection
0074     std::vector<JSValue *> m_objectStack;
0075 
0076     bool m_rootIsUndefined;
0077     bool m_emtpySpacer;
0078 };
0079 
0080 }
0081 
0082 #endif // JSONSTRINGIFY_H